Spring MVC Workflow

Spring MVC Workflow

 

working diagram



 flow chart



 

 

 

Workflow Description

 

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

 

      2. The DispatcherServlet parses the request URL and obtains the request resource identifier (URI). Then according to the URI, call HandlerMapping to obtain all related objects configured by the Handler (including the Handler object and the interceptor corresponding to the Handler object), and finally return it in the form of a HandlerExecutionChain object;

 

      3. DispatcherServlet selects a suitable HandlerAdapter according to the obtained Handler. (Note: If the HandlerAdapter is successfully obtained, the preHandler(...) method of the interceptor will be executed at this time)

       

      4. Extract the model data in the Request, fill in the Handler input parameters, and start executing the Handler (Controller). In the process of filling in the Handler's parameters, according to your configuration, Spring will do some additional work for you:

           HttpMessageConveter: Convert the request message (such as Json, xml and other data) into an object, and convert the object into the specified response information

           Data transformation: perform data transformation on the request message. Such as String conversion to Integer, Double, etc.

           Data Root Formatting: Format the data of the request message. Such as converting strings to formatted numbers or formatted dates, etc.

           Data verification: Verify the validity of the data (length, format, etc.), and store the verification result in BindingResult or Error

      

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

      

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

      

      7. ViewResolver 结合Model和View,来渲染视图

      

      8. 将渲染结果返回给客户端。

 

 

 

spring工作流程的问题

 

1. 为什么Spring只使用一个Servlet(DispatcherServlet)来处理所有请求?

详细见J2EE设计模式-前端控制模式

      你也可以定义多个DispatcherServlet,只不过通常业务场景下,一个就可以了。根据业务需求可以定义多个DispatcherServlet,这也是为什么SpringMVC有两个context甚至更多的原因

 

2. Spring为什么要结合使用HandlerMapping以及HandlerAdapter来处理Handler?

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

 

 

参考:

http://blog.csdn.net/zuoluoboy/article/details/19766131/

Guess you like

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