Spring MVC

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.

Figure 68.1 shows a high level overview of request processing workflow in Spring Web MVC

Figure 68.1 Figure 68.1

Request Processing Workflow

The list below describes the complete process a request goes through when handled by a DispatcherServlet:

  • The Incoming request is recieved.
  • Request is dispatched to the handler.
  • Handler handles the request.
  • DispatcherServlet consults the HandlerMapping to call the appropriate Controller.
  • The Controller takes the request, sets ModelAndView and returns view name to the DispatcherServlet.
  • DispatcherServlet received the ModelAndView and call the viewResolver to process it.
  • viewResolver return the view back to the DispatcherServlet.
  • The DispatcherServlet passes the model data and return the view back to user.

Following example demonstrates a simple Hello World web application using Spring MVC.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Controller

To provide a basic infrastructure, all of Spring’s various Controller inherit from AbstractController. When using the AbstractController as the baseclass for your controllers you only have to override the handleRequestInternal(HttpServletRequest, HttpServletResponse) method, implement your logic, and return a ModelAndView object.


package com.kruders.controller;

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 HelloWorldController extends AbstractController{
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		ModelAndView model = new ModelAndView("helloworld");
		model.addObject("message", "Hello World!!!");
		return model;

2. Create View

Create jsp folder in WEB-INF and create helloworld.jsp file in jsp folder

Now write the following code in helloworld.jsp file


3. Configuration

Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.

<bean name="/helloworld.html" class="com.kruders.controller.HelloWorldController" />
    <property name="prefix">
    <property name="suffix">

4. Integrate Spring in Web App

To integrate Spring in Web Application, write the following code in web.xml


5. Run

Now when you run the project, following screen will be displayed as in Figure 68.2

Figure 68.2 Figure 68.2

The folder structure of the example is shown below in Figure 68.3

Figure 68.3 Figure 68.3

You can download the source code of this example here.


No comments yet.

Leave a Reply