Sei sulla pagina 1di 8

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:

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.

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

This section describes the hierarchy of Spring MVC Module.

Spring MVC Controllers - Controllers hierarchy in Spring MVC


Controllers hierarchy in Spring MVC In this we will will understand the controllers hierarchy in Spring MVC Module. The Spring MVC module provides a lot of flexibility to easily develop MVC based web applications. It provides many controllers that can be used to achieve different jobs. Spring MVC module is based on the MVC design pattern. The main components involved are DispatcherServlet, Controller and Views. In Spring MVC DispatcherServlet plays very important role. It handles the user request and delegates it with Controller. Following diagram shows the very simplified architecture:

In this Spring MVC, DispatcherServlet works as the controller and it delegates the request to the Controller. Developers extends the abstract controller provided by the framework and writes the business logic there. The actual business related processing is done in the Controller. Spring MVC provides many abstract controllers, which is designed for specific tasks. Here is the list of anstract controllers that comes with the Spring MVC module: 1. SimpleFormController 2. AbstractController 3. AbstractCommandController 4. CancellableFormController 5. AbstractCommandController 6. MultiActionController 7. ParameterizableViewController 8. ServletForwardingController 9. ServletWrappingController 10. UrlFilenameViewController 11. AbstractController 12. AbstractCommandController 13. SimpleFormController 14. CancellableFormController. Following diagram shows the Controllers hierarchy in Spring MVC:

In the next sections we will be learning about all these controllers. We will also provide you the examples codes illustrating the usage of these controllers.

Spring MVC Application Flow Diagram


I will explain Flow of typical Spring MVC application or in other words How exactly user gets served?. We will see how data and control flows through all the stages of this process. Let me start with one diagram,

Heres the transition explanation below,


Transition 1 User sends request to server by submitting form / by clicking hyperlink etc. Request is initially given to WEB.XML. Transition 2 WEB.XML routes request to DispatcherServlet by looking at <servletmapping> tag. Transition 3 Inside DispatcherServlet, First HandlerMapping handed over request to suitable Controller. Transition 4 Controller maps request to proper Model class. All BUSINESS LOGIC is done inside Model class. Transition 5 If database operation is needed then Model class will route request to suitable DAO. All database operations should be carried out in DAO.

Transition 6 If needed then attach attributes into request/session/application scope and return back to Model. Transition 7 If needed then attach attributes into request/session/application scope and return back to Controller. Transition 8 Controller simply returns it to any View(JSP/HTML etc). Transition 9 JSP/Html is viewed back to user.

I will post related Spring MVC sample application soon.