- Mastering Spring 5.0
- Ranga Rao Karanam
- 271字
- 2021-07-02 22:12:15
How it works
Key components in the Spring MVC architecture are shown in the following figure:
Let's look at an example flow and understand the different steps involved in executing the flow. We will take flow 4, returning ModelAndView as the specific example. The URL of flow 4 is http://localhost:8080/welcome-model-view. The different steps are detailed as follows:
- The browser issues a request to a specific URL. DispatcherServlet is the Front Controller, handling all requests. So, it receives the request.
- Dispatcher Servlet looks at the URI (in the example, /welcome-model-view) and needs to identify the right controller to handle it. To help find the right controller, it talks to the handler mapping.
- Handler mapping returns the specific handler method (in the example, the welcome method in BasicModelViewController) that handles the request.
- DispatcherServlet invokes the specific handler method (public ModelAndView welcome(ModelMap model)).
- The handler method returns the model and view. In this example, the ModelAndView object is returned.
- DispatcherServlet has the logical view name (from ModelAndView; in this example, welcome-model-view). It needs to figure out how to determine the physical view name. It checks whether there are any view resolvers available. It finds the view resolver that was configured (org.springframework.web.servlet.view.InternalResourceViewResolver). It calls the view resolver, giving it the logical view name (in this example, welcome-model-view) as the input.
- View resolver executes the logic to map the logical view name to the physical view name. In this example, welcome-model-view is translated to /WEB-INF/views/welcome-model-view.jsp.
- DispatcherServlet executes the View. It also makes the Model available to the View.
- View returns the content to be sent back to DispatcherServlet.
- DispatcherServlet sends the response back to the browser.