About Spring MVC
Figure Description:- A registration request is sent by DispatchServlet to the
RegsitrationController(as figure out or directed by BeanNameUrlHandlerMapping).
As a result, InternalResourceViewResolver directs the request to reg.jsp to
render the page output.
Spring MVC flow steps:-
- DispatcherServlet receives a request whose URL pattern is /reg.htm. This is the first stop in the request’s travels. This works on the Front Controller design pattern. It is a common web-application pattern where a servlet delegates responsibility for a request to other components of an application to perform the actual processing. Here the DispatcherServlet job is to send the request on to a spring MVC controller. A controller is nothing but a spring component that processes the request. But an application contains multiple controllers and then DispatcherServlet needs help to decide which controller can able to serve the coming request.
- DispatcherServlet consults BeanNameUrlHandlerMapping(as per the above case) to find the corresponding controller whose bean name is /reg.htm and it finds the RegistrationController bean.
- DispatcherServlet dispatches the request to RegistrationController for processing.
- The logic performed by a controller often results in some required information that needs to be carried back to the client/user and displayed in the browser. This information is reffered to as the model. But sending the raw information is not the sufficient and correct way. It needs to be formatted in a user-friendly format. So the last thing that the controller will do is packaging the model data and the name of a view into a ModelAndView object. RegistrationController then returns a ModelAndView object with a logical view name that is reg.jsp. As the name implies, ModelAndView object containsboth the model data as well as a hint to what view should be returned to render the screen.
- The ModelAndView object does not contain the actual JSP, it only carries a logical name that will be used to look up the actual view. So once the ModelAndView object is delivered to the DispatcherServlet then the DispatcherServlet take helps of ViewResolver to find the actual JSP. The DispatcherServlet consults its view resolver(configured as InternalResourceViewResolver in the above case) to find a view whose logical name is reg. InternalResourceViewResolver returns the path to /WEB-INF/jsp/reg.jsp.
- Now the DispatcherServlet knows which view will render the results, so the final stop is at the view implementation where it delivers the model data. Now the view will use the model data to render a page that will be carried back to the client by the response object. In the above scenario, DispatcherServlet forwards the request to the JSP at /WEB-INF/jsp/reg.jsp to render the user page as the response.
No comments:
Post a Comment