Search This Blog

Spring MVC


Spring MVC helps in building flexible and loosely coupled web applications. The Model-view-controller design pattern helps in seperating the business logic, presentation logic and navigation logic. Models are responsible for encapsulating the application data. The Views render response to the user with the help of the model object . Controllers are responsible for receiving the request from the user and calling the back-end services.
The figure below shows the flow of request in the Spring MVC Framework.
When a request is sent to the Spring MVC Framework the following sequence of events happen.
  • The DispatcherServlet first receives the request.
  • The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request.
  • The Controller process the request by calling the appropriate service methods and returns aModeAndView object to the DispatcherServlet. The ModeAndView object contains the model data and the view name.
  • The DispatcherServlet sends the view name to a ViewResolver to find the actual View to invoke.
  • Now the DispatcherServlet will pass the model object to the View to render the result.
  • The View with the help of the model data will render the result back to the user.

Struts MVC Architecture





The model contains the business logic and interact with the persistance storage to store, retrive and manipulate data.
The view is responsible for dispalying the results back to the user. In Struts the view layer is implemented using JSP.
The controller handles all the request from the user and selects the appropriate view to return. In Sruts the controller's job is done by the ActionServlet.


The following events happen when the Client browser issues an HTTP request.
  • The ActionServlet receives the request. 
  • The struts-config.xml file contains the details regarding the Actions, ActionForms,ActionMappings and ActionForwards
  • During the startup the ActionServelet reads the struts-config.xml file and creates a database of configuration objects. Later while processing the request the ActionServletmakes decision by refering to this object.
When the ActionServlet receives the request it does the following tasks.
  • Bundles all the request values into a JavaBean class which extends Struts ActionForm class. 
  • Decides which action class to invoke to process the request. 
  • Validate the data entered by the user. 
  • The action class process the request with the help of the model component. The model interacts with the database and process the request. 
  • After completing the request processing the Action class returns an ActionForward to the controller. 
  • Based on the ActionForward the controller will invoke the appropriate view. 
  • The HTTP response is rendered back to the user by the view component.