Saturday, February 9, 2013

How Spring MVC works

MVC is a very good design pattern which is widely used in applications with UI(Desktop or web).   In this pattern M is Model, V is the View and C is controller.   The advantage of design pattern is to separate the different responsibilities into 3 parts when dealing UI.   In general terms  Model represents the business data and logic, View is visual part where the data in Model is displayed and Controller is the link of Model and View.  Controller is the brain which decides what data is used and how data is viewed.    Each part in MVC has the clear and dedicated responsibly.   By using MVC pattern the applications with UI become easier to develop, modify and maintain. 


SpringMVC is a web framework provided by Spring based on MVC pattern.   This framework is built on top of JEE/Servlet and is request-driven.  So it means that some servelts will listen on some ports for the incoming request and each request will trigger the whole process of serving this request in MVC and the data or resources that the request asks for is presented.
The below shows the categories of SpringMVC using MVC terms.



In Spring MVC DispatcherServlet and Controllers act as the Controller and they receive the requests and decide how the request will be served and also decide which view is used.  Business objects and domain objects are the Model and the business objects are invoked by the controllers and the all data are in domain objects.   The domain objects will be passed to some JSP which are the View part.  The JSP will render the data in the domain objects.

DisptacherServlet

DispatcherServlet is the front controller in Spring MVC.   It is a HttpServlet that will receive the request and return the response.  DispatcherServlet is the key player in SpringMVC.  From the below brief work flow of SpringMVC you can see DispatcherServlet is the driving force that make the request served with the right response sent back to the client.    


  1. Receive the request from client
  2. Consult HandleMapping to decide which controller processes the request
  3. Dispatch the request to the controller   
  4. Controller processes the request and returns the logical view name and model back to DispatcherServlet
  5. Consult ViewResolver for appropriate View for the logical view name from Controller
  6. Pass the model to View implementation for rendering
  7. View renders the model and returns the result to DispatcherServlet
  8. Return the rendered result from view to the client
It is obvious that DispatcherServlet has a heavy workload.  It needs many other strategy objects and configuration to perform these work.   Each DispatcherServlet has its own specialized ApplicationContext: WebApplicationContext.


MappingHandler


One important work to be done before DispatcherServlet can dispatch the request to the Controller is to find out which controller is right one for this request.  DispatcherServlet uses MappingHandler strategy object to do so.   There are many MappingHandler implementations which uses different strategies to map the request to Controller.   By default DispatcherServlet will use BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping.

public interface HandlerMapping {
      HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;
}


Controller

After the mapping is resolved DispatcherServlet will dispatch the request to the Controller.  Controller does the real work of processing the request.  Also Controller is where programmers need to work most in developing SpringMVC applications.  Controller has the knowledge of processing the request and what logical view be used for different result of request processing.  Usually Controller doesn't do the real processing and it delegates the request processing to the service layer.   Another thing the Controller to do is to package the result of the processing into the Model, which will be rendered in the View finally.


ViewResolver

After the Controller finish the processing of request it will return the logical view name and the data to DispatcherServlet, which will decide the actual view to be used since the view name from Controller is logical name.   With the help of ViewResolver strategy object DispatcherServlet can find out physical view from the logical view name.   Similar to MappingHandler there are also many different strategies  for resolving the view based on the different view technologies.    Most commonly used implementation of ViewResolver is InternalResourceViewResolver.  

public interface ViewResolver {
      View resolveViewName(String viewName, Locale locale) throws Exception;
}


View

View is where the data in the Model is rendered as the required output for the client.   SpringMVC provides many implementations of View to generate different output such as JSP,Excel, PDF, XML and etc.   DispatcherServlet will invoke render method from selected View implementation to generate the output to be returned to the client.

public interface View {
      String getContentType();
      void render(Map<String, ?> model, HttpServletRequest request,  HttpServletResponse response) throws Exception;
}

8 comments:

  1. Good article, very easy to follow. Thanks!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. It's well written and provides a nice overview of the implementation of MVC pattern in Spring!

    One problem I have: steps #7 and #8 in the DisptacherServlet section contradict the fact that render() returns void. Can you please clarify if the DisptacherServlet indeed returns "the rendered result from view to the client"' or' if the View responds directly to the HttpServletRequest without going back through the DispatcherServlet.

    Thanks!

    ReplyDelete
  4. Just Perfect! Liked the way Spring MVC is explained

    ReplyDelete
  5. One correction, Its the HandlerMapping not the MappingHandler interface.

    ReplyDelete
  6. nice explanation. thanks a lot

    ReplyDelete