Difference between SpringMVC and Struts2

1. Framework mechanism

1. Struts2 is implemented by Filter (StrutsPrepareAndExecuteFilter), and SpringMVC (DispatcherServlet) is implemented by Servlet.
2. The Filter is initialized after the container is started; it crashes after the service is stopped, later than the Servlet. The servlet is initialized when it is called, before the Filter call, and destroyed after the service stops.


Second, the interception mechanism

1、Struts2

a. The Struts2 framework is a class-level interception. Each request will create an Action. When integrating with Spring, the ActionBean injection scope of Struts2 is the prototype mode prototype (otherwise there will be thread concurrency problems), and then request data through setters and getters. Inject into properties.
b. In Struts2, an Action corresponds to a request and response context. When receiving parameters, it can be received through attributes, which means that attribute parameters are shared by multiple methods.
c. A method of Action in Struts2 can correspond to a url, but its class attributes are shared by all methods, which means that it cannot be identified by annotations or other methods. 2. SpringMVC a, SpringMVC are method-level interception, a The method corresponds to a Request context, so the method is basically independent and has exclusive request and response data. And each method corresponds to a url at the same time, and the transfer of parameters is directly injected into the method, which is unique to the method. The processing result is returned to the framework through ModeMap. b. During Spring integration, SpringMVC's Controller Bean defaults to Singleton mode, so by default, only one Controller will be created for all requests, and there should be no shared properties, so it is thread-safe. If you want to change the default role domain, you need to add @Scope annotation to modify. 3. In terms of performance, SpringMVC implements zero configuration. Due to the method-based interception of SpringMVC, there is a singleton mode bean injection loaded once. Struts2 is a class-level interception. Each time a new Action is requested for the corresponding instance, all attribute value injections need to be loaded. Therefore, the development efficiency and performance of SpringMVC is higher than that of Struts2. Fourth, the interception mechanism
 










Struts2 has its own interceptor mechanism. SpringMVC uses an independent Aop method, which leads to a larger amount of configuration files in Struts2 than in SpringMVC.


5. In terms of configuration,
spring MVC and Spring are seamless. The management and security of this project are also higher than Struts2 (of course, Struts2 can also achieve the same effect as SpringMVC through different directory structures and related configurations, but there are many places that require xml configuration).
SpringMVC can be considered 100% zero configuration.


Sixth, the design idea
Struts2 is more in line with the OOP programming idea, SpringMVC is more cautious and expands on servlet.


7. In terms of integration,
SpringMVC integrates Ajax, which is very convenient to use. It can be realized with only one annotation @ResponseBody, and then the response text can be returned directly, while the Struts2 interceptor integrates Ajax. When processing in Action, you must generally install plug-ins or yourself It is relatively inconvenient to write code to integrate it.

Guess you like

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