Spring MVC getting started Hello example



DispatcherServlet 

DispatcherServlet class is responsible for loading entire Spring MVC framework

Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters. Dispatches to registered handlers for processing a web request, providing convenient mapping and exception handling facilities.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5">
   
    <!-- ************************************************************* -->
    
  	<display-name>Spring MVC Application</display-name>
  
  	<servlet>
	    <servlet-name>dispatcher</servlet-name>
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	    <load-on-startup>1</load-on-startup>
  	</servlet>
  
  	<servlet-mapping>
	    <servlet-name>dispatcher</servlet-name>
	    <url-pattern>*.htm</url-pattern>
  	</servlet-mapping>
  	
    <!-- ************************************************************* -->  
    
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

    <!-- ************************************************************* -->
    	
</web-app>


Loading spring configuration file

Create a spring config in WEB-INF directory with

prefix as servlet-name given in web.xml and

sufix as -servlet.xml

Example if your servlet-name is dispatcher then your spring config file should be dispatcher-servlet.xml


dispatcher-servlet.xml

handler mapping

    Is responsible for mapping your action name to controller class, there are different types of handler mapping lets start with basic and default handler mapping BeanNameUrlHandlerMapping

View resolver

    Is responsible for loading view page once the controller execution is completed.  there are also different types of view resolver available lets start with basic view resolver InternalResourceViewResolver

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
           http://www.springframework.org/schema/util 
           http://www.springframework.org/schema/util/spring-util-3.0.xsd  
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<!-- ************************************************************* -->
	<!-- which handler mapping used here BeanNameUrlHandlerMapping -->
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
	<bean name="/beanNameUrlHandlerMapping.htm"
		class="com.candidjava.springmvc.BeanNameUrlHandlerMappingController" />
	<!-- ************************************************************* -->
	<!-- which resolver used here InternalResourceViewResolver -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<!-- location of jsp pages -->
			<value>/WEB-INF/pages/</value>
		</property>
		<!-- extension of pages -->
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	<!-- ************************************************************* -->
</beans>


Spring Controller

            Lets start with spring basic controller AbstractController works similar to servlet doGet and doPost, and return a ModelAndView object with pagename and data.

package com.candidjava.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class BeanNameUrlHandlerMappingController extends AbstractController

{

	public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView model = new ModelAndView("page");
		
		model.addObject("msg", "This is BeanNameUrlHandlerMapping example message!");
		
		return model;
	}
}


View page

uses expression language to display the data

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<h3>Cnadid Java BeanNameUrlHandlerMapping Example in Spring MVC</h3>
<h4>${msg}</h4>
</body>
</html>


Download 

Spring MVC hello world zip

Spring MVC hello world war



Related Post

Comments


©candidjava.com