Spring MVC Workflow – Understanding How Spring MVC Mechanism Works

Let’s start by observing the figure below:

how-spring-mvc-mechanism-works

 

First, the request is received by the DispatcherServlet. DispatcherServlet is a front controller. It is like a super controller who redirects all the requests to the corresponding handler methods inside the controller classes.

HandlerMapping is an interface. An implementation of this interface provides the mapping between URL patterns and the handler methods to be invoked for a URL. (Before Spring 3.1, mapping was done in 2 steps. 1) Mapping the controller, 2) Mapping of the handler method. From Spring 3.1 on we have RequestMappingHandlerMapping class which combines the two into a single step. )

HandlerAdapter is used to invoke the target handler methods. DispatcherServlet does not call these handler methods himself but uses HandlerAdapter to do this providing another level of abstraction.

HandlerInterceptor interface can be implemented to add some cross-controller capabilities like logging. (It is also possible to extend HandlerInterceptorAdapter abstract class to achieve the same thing. There are cases where you might like to implement an interface or prefer extending an abstract class. )

Once the request is handled, a ModelAndView object is returned to the DispatcherServlet.

At this point, view to be rendered is resolved by a ViewResolver implementation, View is populated by a Model and result (for example a rendered JSP file) is returned to the client.

 

Hope this helps.
Good Luck,
Serdar