How Spring MVC Module Works

In this section we will understand the Spring MVC Module in detail.



How Spring MVC Works


How Spring MVC Works?

In this we will see the request flow for the spring framework. We will also show you the request flow diagram illustrating the working of Spring MVC module.

The Spring MVC modules comes with the Spring Framework distribution. The Spring MVC modules of the Spring Framework well integrates with rest of the framework. This modules is also very extensible.

Spring MVC is based on the MVC design pattern. Here is the list of key classes of Spring MVC.

  • DispatcherServlet
    The DispatcherServlet is  configured in the web.xml file and required URL patterns are mapped to this Servlet. It works as the front controller and handle all the request from the user.
  • ModelAndView
    This class works as the holder for both Model and View in the Spring MVC.
  • SimpleFormController
    The SimpleFormController is the Concrete FormController implementation. It provides the configurable form and success views, and an onSubmit chain for convenient overriding. Automatically resubmits to the form view in case of validation errors, and renders the success view in case of a valid submission.

Simplified Spring MVC architecture diagram

Following diagram shows the simplified architecture of Spring MVC:

Spring MVC Architecture, Spring MVC request flow

Let's understand the sequences of the events happens when a request is received by the Spring MVC. Following events happens when DispatcherServlet receives are request:

  1.  The DispatcherServlet configured in web.xml file receives the request.
  2. The DispatcherServlet finds the appropriate Controller with the help of HandlerMapping and then invokes associated Controller.
  3. Then the Controller executes the logic business logic (if written by the programmer) and then returns ModeAndView object to the DispatcherServlet.
  4. The DispatcherServlet determines the view from the ModelAndView object.
  5. Then the DispatcherServlet passes the model object to the View.
  6.  The View is rendered and the Dispatcher Servlet sends the output to the Servlet container. Finally Servlet Container sends the result back to the user.


Request flow in Spring MVC

Spring MVC is request driven and DispatcherServlet handles the request from client and then dispatches the request to controllers. It tightly integrates with the Spring IoC container and allows the developers to use every features of Spring framework.

The following diagram illustrates the request flow in Spring MVC.

Spring MVC Request flow

Request handling steps in Spring MVC

  1. Client access some URL on the server.
  2. The Spring Front Controller (DispatcherServlet) intercepts the Request. After receiving the request it finds the appropriate Handler Mappings.
  3. The Handle Mappings maps the client request to appropriate Controller. In this process framework reads the configuration information from the configuration file or from the annotated controller list. Then DispatcherServlet dispatch the request to the appropriate Controller. The Handler Adapters involves in this process.
  4. Then the Controller processes the Client Request, it executes the logic defined in the Controller method and finally returns the ModelAndView object back to the Front Controller.
  5. Based on the values in the ModelAndView Controller resolves the actual view, which can be JSP, Velocity, FreeMaker, Jasper or any other configured view resolver.
  6. Then the selected view is rendered and output is generated in the form of HttpServletResponse. Finally Controller sends the response to the Servlet container, which sends the output to the user.

In the next section we will see the controller stack in Spring MVC

Share on Google+Share on Google+

How Spring MVC Module Works

Posted on: August 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: How Spring MVC Module Works  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 15, 2013

Hi there! I just wanted to ask if you ever have any isesus with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no data backup. Do you have any methods to protect against hackers?
September 14, 2011
Thanks :)

Excellent Explanation for a beginner , thanks :) :)
February 24, 2012
i need description of spring AOP module

when should we use spring AOP module in realtime.plse help me.
October 11, 2012
SpringMVC Request Processing flow.

Please Explain how DispatcherServlet finds the appropriate HandlerMapping or appropriate Controller when we submit any form with data?
sumit sharma
June 27, 2014
regarding processing flow of command class

I have a very bad confusion In spring MVC I use println method in every statement of command class setter and getter method I have a home page where I have use a link to go to login page when I click on link then control forward to login page and there is only two fileld I have declared so why getter method of user name and password called 4-4 times ..pls help me out.