Spring MVC workflow flowchart

FIG. 1




Figure 2




Spring workflow description

1. The user sends a request to the server, which is captured by Spring front-end control Servelt DispatcherServlet.

2. DispatcherServlet parses the request URL and obtains the request resource Identifier (URI). Then, according to the URI, HandlerMapping is called to obtain all the related objects (including the Handler object and the interceptor corresponding to the Handler object) configured by the Handler. Finally, HandlerExecutionChain object is returned.

3. The DispatcherServlet selects an appropriate HandlerAdapter according to the obtained Handler. (Note: If the HandlerAdapter is successfully obtained, execution of the interceptor’s preHandler(…) begins at this point.) Methods)

4. Extract the model data from the Request, fill in the Handler input parameter, and run Handler (Controller). Depending on your configuration, Spring will do some extra work for you during the entry process to populate the Handler:

HttpMessageConveter: Converts the request message (such as Json or XML data) into an object, which is converted into the specified response information

Data transformation: Data transformation of the request message. Such as String to Integer, Double, and so on

Data root: Data formatting of the request message. For example, converting a string to a formatted number or a formatted date

Data validation: Verifies the validity of data (length, format, etc.) and stores the validation results in BindingResult or Error

5. Handler returns a ModelAndView object to DispatcherServlet after execution.

6. According to the returned ModelAndView, select a suitable ViewResolver (must be a ViewResolver registered in Spring container) and return it to DispatcherServlet;

7. ViewResolver combines Model and View to render the View

8. Return the rendering result to the client.

Spring workflow description

Why does Spring only use one Servlet(DispatcherServlet) to handle all requests?

See J2EE design Patterns – Front-end control patterns for details

Why does Spring use HandlerMapping and HandlerAdapter to handle handlers?

It conforms to the principle of single responsibility in object-oriented, and the code architecture is clear, easy to maintain, and most importantly, the code is highly reusable. A HandlerAdapter, for example, may be used to handle multiple handlers.

Classic introduction:

Source structure



Welcome everyone to study the relevant technology together willing to understand the framework technology or source code of friends directly add beg: 2042849237

More detailed source code reference source: minglisoft.cn/technology