This article mainly introduces the difference between struts2 and spring MVC in Java. It is very good and has reference value. Friends who need it can refer to it
1. Struts2 is a class-level interception. One class corresponds to A request context, SpringMVC is a method-level interception, a method corresponds to a request context, and the method also corresponds to a url, so from the architecture itself, SpringMVC can easily implement restful url, while the struts2 architecture is difficult to implement because A method of Action in Struts2 can correspond to a URL, but its class attributes are shared by all methods, which makes it impossible to use annotations or other methods to identify its own method.
2. SpringMVC methods are basically independent and have exclusive access to request response data. The request data is obtained through parameters, and the processing results are returned to the framework through ModelMap. No variables are shared between methods.
3. Since Struts2 needs to encapsulate each request, encapsulate the variables of the servlet life cycle such as request and session into Maps for use by each Action and ensure thread safety, so in principle, It consumes more memory.
4. In terms of interceptor implementation mechanism, Struts2 has its own interceptor mechanism, while SpringMVC uses an independent AOP method. This results in the amount of configuration files of Struts2 being larger than that of SpringMVC.
5. The entrance of SpringMVC is servlet, while Struts2 is filter (it should be pointed out here that filter and servlet are different. In the past, filter was considered to be a special type of servlet), which leads to different mechanisms of the two. , here involves the difference between servlet and filter.
6. SpringMVC integrates Ajax, which is very convenient to use. It can be implemented with just one annotation @ResponseBody, and then directly returns the response text. The Struts2 interceptor integrates Ajax, which is generally required when processing in Action. Installing plug-ins or writing your own code to integrate them is relatively inconvenient to use.
7. SpringMVC verification supports JSR303, which is relatively more flexible and convenient to handle, while Struts2 verification is more cumbersome and feels too confusing.
8. 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).
9. In terms of design ideas, Struts2 is more in line with OOP programming ideas, while SpringMVC is more cautious and expands on servlets.
10. SpringMVC performance is higher than Struts2.
11. SpringMVC can be considered to have 100% zero configuration.
The above is the detailed content of Compare the usage differences between struts2 and spring MVC in Java. For more information, please follow other related articles on the PHP Chinese website!