Request processing workflow in Spring Web MVC :

Image courtesy: springsource.org
Request processing workflow in Spring Web MVC
  1. Front Controller (DispatcherServlet) receives requests based on the url-pattern mentioned inside servlet-mapping in web.xml.

  2. Based on HandlerMapping (map of URL and Controller), Front Controller delegates the request to the matching controller.

  3. Handler method of the matching controller is responsible for processing the delegated request and returns ModeAndView to the Front Controller (DispatcherServlet).

  4. DispatcherServlet resolves the logical view name to actual view using ViewResolver and passes the Model object to the view.

  5. Response is prepared using the Model and actual view. Then the control goes back to the DispatcherServlet.

  6. DispatcherServlet returns the response which gets rendered in the browser.

 

Tools and Technologies used in this article :

  1. Spring 3.1

  2. JDK 1.6

  3. Eclipse 3.7

  4. Maven Integration for Eclipse WTP (a.k.a m2e-wtp)

  5. Tomcat 7

 

Note : Spring 3 requires at least JDK 5. So, make sure you have JDK 5 or above.

1. Create a Java Web Project using m2e

Select from the menu File --> New --> Other --> Maven --> Maven Project. Browse to the workspace location and click 'Next' button.

Select project name and location

Type 'webapp' in the 'Filter' field and select archetype 'maven-archetype-webapp' to generate a simple java web application using Maven. Click 'Next' button.

Select an Archetype

Specify archetype parameters (Group Id, Artifact, Version and Package) and click 'Finish' button.

Specify Archetype parameters

 

2. Add Project Dependencies in pom.xml

Add dependency of Spring MVC 3 in Maven pom.xml.

File : pom.xml
Note : Spring MVC depends on spring-core, spring-beans, spring-context, spring-web. If we add 'spring-webmvc' dependency in pom.xml then all the required dependencies / libraries will be automatically downloaded and added to the project's classpath by Maven.

 

3. Controller Class and Request Mapping

Create a spring controller class called SpringMVCHelloController in package 'com.srccodes.spring.controller' and copy following code into it.

File : SpringMVCHelloController.java

@Controller annotation indicates that this class serves the role of a controller. The Servlet dispatcher scans @Controller annotated Controller classes for mapped handler methods and detects @RequestMapping annotations.
@RequestMapping annotation specifies that this handler method will process all requests beginning with '/' in the URL and return the logical view name.
'org.springframework.ui.Model' object is a map which is passed by the Spring container while invoking the handler method 'printHelloWorld'. The message string 'Hello World!' has been added to 'Model' object against the key 'message'. This model object is available in the view. We can use the key 'message' to get the value and display the same in the UI.

 

4. Create a JSP page as View

Now we need to create a JSP page called helloWorld.jsp under 'WEB-INF/pages' directory and copy the following jsp file content. We have used the key 'message' in expression '${message}' to get the value and display the same in 'helloWorld.jsp'.

File : helloWorld.jsp

 

5. Add Spring Configuration File

Create an xml file called dispatcher-servlet.xml under 'WEB-INF' directory and copy the following content.

File : dispatcher-servlet.xml

The above configuration file provides context information to the Spring container. '<context:component-scan>' enables the auto-detection of annotated components (like Controller, Service etc). So spring container will scan and load all the annotated component (e.g. SpringMVCHelloController) from the package 'com.srccodes.spring.controller' and it's subpackages.

'<mvc:annotation-driven>' specifies that we are using annotation based configuration.

ViewResolver provides a mapping between logical view name and actual view. It enables us to render models in UI without tying us to a specific view technology like JSPs, JSF, Velocity templates etc. Handler method defined in Controller class must resolve to a logical view name. In our case, 'printHelloWorld' handler method returns 'helloWorld' view name. It gets resolved to the path 'WEB-INF/pages/helloWorld.jsp' by the 'InternalResourceViewResolver' by adding prefix '/WEB-INF/pages/' and suffix '.jsp' to the view name 'helloWorld'.

 

6. Integrate Spring with Web Application

Copy the following content in your web.xml file.

File: web.xml

DispatcherServlet (Front Controller) is mapped with the <url-pattern> '/'. Upon initialization of the DispatcherServlet, Spring MVC looks for a configuration file called [servlet-name]-servlet.xml under the WEB-INF directory and creates the beans defined there. In our case the file is 'dispatcher-servlet.xml'.

 

7. Overall Project Structure

Overall Project Structure

 

8. Deploy and Run the Application

Right click on the project 'SpringMVCHelloWorld' and select from context menu 'Run As' --> 'Run on Server'. Select the existing tomcat server. If not available then manually define a new web server.Click "Finish" button. The web application will be deployed in the tomcat web server and a browser will be opened with 'Hello World!' message as shown below

http://localhost:8080/SpringMVCHelloWorld
Browser Output

 

Download Source Code

 

References