Search This Blog


Sunday, 4 November 2012

Basic MVC application in Spring

Spring provides support for MVC based web application development. I decided to create a very basic application involving a simple HTML page and a JSP page.
I created a simple dynamic web project using eclipse:
This implementation uses zero annotations and simply attempts to develop a very basic  MVC application focusing on the actual flow of control within Spring.
The first step is to define the DispatcherServlet. Spring's web support is built around the front controller pattern. This requires the definition of a single servlet which receives all requests and then delegates the responsibility of processing theses requests to appropriate controllers. In Spring we have the front controller servlet predefined as org.springframework.web.servlet.DispatcherServlet.
We simply need to configure it in our web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
   xsi:schemaLocation=" " 
We have defined that all requests ending with .do are redirected to the servlet.
When the servlet container loads the DispatcherServlet it
  1. executes the init() method as a part of Servlet lifecycle. The DispatcherServlet inherits its implementation from org.springframework.web.servlet.HttpServletBean. The call of execution is made to the abstract method initServletBean()
  2. the abstract method is implemented by org.springframework.web.servlet.FrameworkServlet. The method is responsible for loading the WebApplicationContext for this application. (via the initWebApplicationContext() method.
  3. the WebApplicationContext is loaded via an XML file with name as <dispatcher servlet name>-servlet.xml. In this case springDispatcher-servlet.xml.
The hierarchy is as below:
Consider the XML configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi=""

      <bean id="urlMapping"
        <property name="mappings">
    <bean id="defaultController" class="com.mvc.controller.WelcomeController"></bean>

    <bean id="viewResolver"
        class=" org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix">
The first part is the Controller. In this case we have created a simple controller bean by name "defaultController".
public class WelcomeController implements Controller {

    public ModelAndView handleRequest(final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = null;
        modelAndView = new ModelAndView("welcome");
        return modelAndView;

The Controller implements the handleRequest method that receives a request and response object. The controller can process the request and generate a suitable response. It then forwards the response to a suitable view which will be displayed.(Implementing the controller is not needed anymore with the arrival of annotations.) But how does the controller decide which view ?
This is where the ModelAndView return value comes into picture. The class is a holder for both Model and View in the web MVC framework. It returns to the controller both model and view in a single return value.
In the above code we do not have any model data. We simply return the view to be used.
The Servlet now needs to decode the view. For that I used a ViewResolver. The job of the view-resolver is to convert a view name into an actual view.
As our view is a jsp file , I used a simple InternalResourceViewResolver class that converts welcome into welcome.jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Spring MVC Minimal</title>
        This is the minimal view as JSP
This still leaves one question open. How does the Servlet select the controller to use ?
For this we defined a UrlMapping object. We specified in our xml file that requests ending in "" must be sent to the bean with name "defaultController".
We shall look into the details of this object in later posts.
The last thing was the welcome page which simply redirects to the controller
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        <title>Spring MVC Minimal</title>
        Redirect application follow from 
        <a href="/SpringMVCMinimal/">here</a>
On deploying the application:
The Welcome page
In hitting the link we can see the JSP output:
The jsp after being resolved by a View Resolver
Thus the steps that occur when a request is made is as below:
  1. Request received by DispatcherServlet
  2. Dispatcher servlet identifies the controller to delegate the request to.
  3. Controller on receiving request processes it and generates the model objects to be sent as data and also the view to display the data.
  4. DispatcherServlet identifies the view to use based on returned value from Controller.
  5. It passes the models to the view which is then finally sent back as response to the client.

No comments:

Post a Comment