Spring MVC Tutorial With Example Using Eclipse And Tomcat [download simple spring web mvc application]

Spring MVC Example :

Project Structure of the example is shown below.

index.jsp :

It is the welcome page where user enters his name and clicks the submit button. ‘action’ attribute (action=”requestURI.htm”) of form tag specifies URI pattern which is used by DispatcherServlet to identify the controller which will process the generated request (All the request generated at this jsp will be processed by HelloWorldController as its name attribute matches the “requestURI.htm”’ in dispatcher-servlet.xml ).

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="requestURI.htm" method="post">
Name : <input type="text" id="textId" name="textName"/>
<input type="submit"/>
</form>
</body>
</html>

web.xml :

This is the xml which is loaded by the container (tomcat) at the star-up . Here we have defined Spring’s DispatcherServlet, all the requests are first handled by this servlet .Then depending upon the request, DispatcherServlet identifies which controller to call for processing the generated request. DispatcherServlet uses a file where we have defined controllers and configuration to resolve views . The name of this file is formed by concatenating value of element and “-servlet.xml”
i.e. “dispatcher” + ”-servlet.xml” (<servlet-name>dispatcher</servlet-name>).

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>spring-web-mvc-new</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>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

dispatcher-servlet.xml :

In this file beans (controllers) and view resolver configurations are defined. In bean tag, attribute ‘name’ is used to specify the URI to map to the controller defined by class attribute. e.g. com.thecafetechno.HelloWorldController is mapped with the http request with URI “/requestURI.htm”.
By default DispatcherServlet uses the BeanNameUrlHandlerMapping to map request with controllers ( BeanNameUrlHandlerMapping uses name of bean to match URL pattern . )
Spring’s InternalResourceViewResolver class is defined here , used to resolve the views. In this viewResolverBean prefix and suffix of the views are mentioned and view name is returned by controller. e.g. “/WEB-INF/jsp/” + ”viewNameReturnedByController” + “.jsp” (/WEB-INF/jsp/result.jsp).

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="viewResolverBean" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
   </bean>
   <bean name="/requestURI.htm" class="com.thecafetechno.HelloWorldController" >
        <property name="processor" ref="processorBean"/>
    </bean>
    <bean name="processorBean" class="process.Processor" />
</beans>