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"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<form action="requestURI.htm" method="post">
Name : <input type="text" id="textId" name="textName"/>
<input type="submit"/>

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">

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"
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">
        <property name="suffix">
   <bean name="/requestURI.htm" class="com.thecafetechno.HelloWorldController" >
        <property name="processor" ref="processorBean"/>
    <bean name="processorBean" class="process.Processor" />