[14] The working principle and difference between struts2 and spring mvc

table of Contents

One, the working principle of Struts2

Second, the working principle of Spring MVC

1) The overall process

2) Core process

Three, the difference between Struts2 and Spring

One, the working principle of Struts2

1. The client (HttpServletRequest) sends a request

2. The request passes through the filter ((optional filter) --> StrutsPrepareAndExecuteFilter (core filter, the core filter of struts1 is FilterDispatcher only filters requests with the suffix .action))

3. The request arrives at ActionMapper, which determines which Action to call

4. After deciding to call an Action, the request will pass through the core filter, and the core filter will hand over the processing of the request to ActionProxy

5. ActionProxy asks the configuration file (Struts.xml) through ConfigurationManager to find the Action class to be called.

6. Then ActionProxy creates a reverse instance of ActionInvocation

7. After the interceptor, call the real Action through the instance of ActionInvocation

8. After Action is executed, ActionInvocation creates Result and returns

9. After the Result passes through the JSP and interceptor, it is returned to the client through HttpServletResponse 

Second, the working principle of Spring MVC

1) The overall process

 ​​ 

1. First, the user sends a request to the front controller, and the front controller decides which page controller to select for processing according to the request information (such as URL) and delegates the request to it, which is the control logic part of the previous controller; 1,2 steps;

2. After the page controller receives the request, it performs functional processing. First, it needs to collect and bind the request parameters to an object. This object is called the command object in Spring Web MVC, and it is verified, and then the command object is delegated to the business object. Processing; return a ModelAndView (model data and logical view name) after processing; steps 3, 4, and 5 in the figure;

3. The front controller takes back control, and then selects the corresponding view for rendering according to the returned logical view name, and transmits the model data for the view rendering; steps 6 and 7 in the figure;

4. The front controller takes back the control right again and returns the response to the user, step 8 in the figure; this is the end of the whole.

2) Core process

Specific steps:

Step 1: The client initiates a request from the browser to the front controller (DispatcherServlet)

Step 2: The front controller requests HandlerMapping to find the Handler (you can find it according to the xml configuration and annotations)

The third step: The processor mapper HandlerMapping returns Handler to the front controller. HandlerMapping will map the request to a HandlerExecutionChain object (including a Handler processor ( page controller Controller? ) object, multiple HandlerInterceptor interceptor objects), through this Strategy mode, it is easy to add new mapping strategies

Step 4: The front controller calls the processor adapter to execute the Handler

Step 5: The processor adapter HandlerAdapter will execute the Handler according to the result of the adaptation

Step 6: Handler execution is complete and returns ModelAndView to the adapter

Step 7: The processor adapter returns ModelAndView to the front controller (ModelAndView is a low-level object of the springmvc framework, including Model and view)

Step 8: The front controller requests the view resolver to perform view resolution (resolved into a real view (jsp) according to the logical view name). Through this strategy, it is easy to replace other view technologies, just change the view resolver.

Step 9: The view resolver returns View to the front controller

Step 10: The front controller performs view rendering (view rendering fills the model data (in the ModelAndView object) into the request field)

Step 11: The front controller responds to the user with the result

Three, the difference between Struts2 and Spring

1. The entrance of spring mvc is a servlet, and the entrance of struts2 is a filter.

2. The performance of spring mvc is higher than struts2 because of the existence of value stack in struts2.

3. Spring mvc integrates Ajax, just need @ResponseBody annotation. The struts2 using ajax is not so convenient.

4. The Struts2 framework is a class-level interception. Each request creates an Action, and the data in the request domain is shared between methods. It can only use the multi-case mode to manage the Action. Spring mvc is the method-level interception, and the requset is exclusively shared between methods. Data, you can use the singleton mode to manage the Controller.

 

Guess you like

Origin blog.csdn.net/Jack_PJ/article/details/88039256