Overview of how SpringMVC works

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 entire 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 call the corresponding method of this object to obtain the Handler object and its corresponding interceptor according to the URI. (Here, we only 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, 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、Handler执行完毕后返回一个ModelAndView对象给DispatcherServlet。

 

6、这个ModleAndView只是一个逻辑视图,并不是真正的视图,DispatcherServlet通过ViewResolver视图解析器将逻辑视图转化为真正的视图(通俗理解为将视图名称补全,如加上路径前缀,加上.jsp后缀,能指向实际的视图)。

 

7、DispatcherServlet通过Model将ModelAndView中得到的处数据解析后用于渲染视图。将得到的最终视图通过http响应返回客户端。

 

概念解析:

1、HandlerMapping 
spring mvc 使用HandlerMapping来找到并保存url请求和处理函数间的mapping关系。 
  
以DefaultAnnotationHandlerMapping为例来具体看HandlerMapping的作用 
  DefaultAnnotationHandlerMapping将扫描当前所有已经注册的spring beans中的@requestmapping标注以找出url 和 handler method处理函数的关系并予以关联。 

2、Handleradapter 
Spring MVC通过HandlerAdapter来实际调用处理函数。 
以AnnotationMethodHandlerAdapter为例 
DispatcherServlet中根据handlermapping找到对应的handler method后,首先检查当前工程中注册的所有可用的handlerAdapter,根据handlerAdapter中的supports方法找到可以使用的handlerAdapter。通过调用handlerAdapter中的handle方法来处理及准备handler method中的参数及annotation(这就是spring mvc如何将reqeust中的参数变成handle method中的输入参数的地方),最终调用实际的handle method。

 

其他问题:

Spring为什么要结合使用HandlerMapping以及HandlerAdapter来处理Handler?
    符合面向对象中的单一职责原则,代码架构清晰,便于维护,最重要的是代码可复用性高。如HandlerAdapter可能会被用于处理多种Handler。

 

时序图(转载):

Guess you like

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