Spring MVC principle and Spring MVC Principle
Spring MVC Workflow
Figure 1
Figure 2
Spring workflow description1. the user sends a request to the server. The request is captured by the Spring front-end control Servelt DispatcherServlet; 2. DispatcherServlet parses the request URL to obtain the request Resource Identifier (URI ). Then, according to the URI, call HandlerMapping to obtain all the related objects (including the Handler object and the interceptor corresponding to the Handler object) of the Handler configuration, and finally return the result in the form of the HandlerExecutionChain object; 3. dispatcherServlet selects a suitable HandlerAdapter Based on the obtained Handler. (Notes: If HandlerAdapter is obtained successfully, the preHandler (...) of the interceptor will be executed (...) method) 4. extract the model data in the Request, fill in the Handler input parameter, and start to execute Handler (Controller ). During the Handler input parameter filling process, Spring will help you with some additional work according to your configuration: HttpMessageConveter: Send request messages (such as Json and xml data) convert an object to a specified response data conversion: Convert the request message data. For example, convert String to Integer or Double: format the request message data. For example, convert a string to a formatted number or a formatted date for data verification: verify the validity (length, format, etc.) of the data, the verification results are stored in BindingResult or Error 5. after Handler completes execution, return a ModelAndView object to DispatcherServlet; 6. based on the returned ModelAndView, select a suitable ViewResolver (which must be ViewResolver already registered in the Spring container) and return it to DispatcherServlet; 7. viewResolver uses Model and View to render a View. returns the rendering result to the client.
Spring workflow description why does Spring use only one Servlet (DispatcherServlet) to process all requests? For details, see the J2EE design mode. Why does Spring use HandlerMapping and HandlerAdapter to process Handler? In line with the single responsibility principle in Object-Oriented Systems, the Code architecture is clear and easy to maintain. The most important thing is the high reusability of code. For example, HandlerAdapter may be used to process multiple Handler types.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.