Interview Question: Workflow of SpringMVC

SpringMVC is the most mainstream Web MVC framework today. There is no one. To be a qualified JavaWeb engineer, it is imperative to learn it well!

Different from the principle of Struts2, SpringMVC realizes the encapsulation of the framework source code and the control of the whole process through the most basic and traditional servlet, while Struts2 realizes the correspondence between the URL path and the specific Action through the filter. (For the specific mechanism of Struts2, see another blog link )

The following figure is a schematic diagram of the principle of springMVC:

SpringMVC workflow overview:

1. The client sends an http request to the web server (such as tomcat), and the web server parses the http request. If the parsed URL address matches the mapping path of DispatcherServlet (configured through servlet-mapping in web.xml), web The container hands the request to the DispatcherServlet for processing.

 

2. After the DispatcherServlet receives the request, it parses the URL to obtain the request resource identifier (URI). Then call the HandlerMapping object obtained by the corresponding method, and then according to the URI, call the corresponding method of this object to obtain the Handler object and its corresponding interceptor. (Here, we just get the Handler object and do not operate it. In SpringMVC, the Handler is called and controlled through the HandlerAdapter)

 

3. The DispatcherServlet selects a suitable HandlerAdapter according to the obtained Handler object, creates its instance object, and executes the preHandler() method in the interceptor.

 

4. In the interceptor method, extract the data model in the request and fill in the Handler input parameters, so all preparations have been done, and start executing the Handler (the controller code we wrote cannot be directly executed, and the operations just now are required , can be transformed into Handler to be executed).

 

5. After the Handler is executed, it returns a ModelAndView object to the DispatcherServlet.

 

6. This ModelAndView is just a logical view, not a real view. DispatcherServlet converts the logical view into a real view through the ViewResolver view parser (commonly understood as completing the view name, such as adding a path prefix and adding a .jsp suffix , which can point to the actual view).

 

7. The DispatcherServlet uses the Model to parse the data obtained in the ModelAndView and use it to render the view. Return the resulting final view to the client via an http response.

 

Concept analysis:

1. HandlerMapping 
Spring mvc uses HandlerMapping to find and save the mapping relationship between url requests and processing functions. 
  
Take DefaultAnnotationHandlerMapping as an example to see the role of HandlerMapping in detail. 
  DefaultAnnotationHandlerMapping will scan the @requestmapping annotations in all currently registered spring beans to find out the relationship between url and handler method and associate them. 

2. Handleradapter 
Spring MVC actually calls the handler function through the HandlerAdapter. 
Taking AnnotationMethodHandlerAdapter as an example 
, after finding the corresponding handler method according to handlerrmapping in DispatcherServlet, firstly check all available handlerAdapters registered in the current project, and find the available handlerAdapters according to the supports method in handlerAdapter. The parameters and annotations in the handler method are processed and prepared by calling the handle method in the handlerAdapter (this is how spring mvc turns the parameters in the reqeust into the input parameters in the handle method), and finally the actual handle method is called.

 

other problems:

Why does Spring use HandlerMapping and HandlerAdapter to process Handler?
    It conforms to the single responsibility principle in object-oriented, the code structure is clear, easy to maintain, and the most important thing is high code reusability. Such as HandlerAdapter may be used to handle a variety of Handler.

 

Timing diagram (reproduced):

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325385431&siteId=291194637