spring struts2 difference

[Java technology] Finally, there is a comparison between SpringMvc and Struts2

Update time: 2015/03/18 11:48 Source: Source Code Education

At present, the proportion of SpringMvc used in enterprises has far exceeded Struts2, so what is the difference between the two, which is a problem that many beginners are concerned about. Let's compare SpringMvc and Struts2 in various aspects:

1. Core controller (front-end controller, preprocessing controller): This word should not be unfamiliar to those who have used the mvc framework. The main purpose of the core controller is to process all requests, and then to those special requests. (Controller) Unified processing (character encoding, file upload, parameter acceptance, exception handling, etc.), the core controller of spring mvc is Servlet, and Struts2 is Filter.

2. Controller instance: Spring Mvc will be faster than Struts (in theory). Spring Mvc is based on method design, while Sturts is based on objects. Each time a request is made, an action will be instantiated, and each action will be injected with   attributes. Spring is more like Servlet, with only one instance, and the corresponding method is executed for each request. Yes (note: since it is a singleton instance, the modification of global variables should be avoided, which will cause thread safety problems).

3. Management method: Spring is used in the core architecture of most companies, and spring mvc is a module in spring, so spring is simpler and more convenient for spring mvc controller management, and provides a full annotation method For management, the annotations of various functions are relatively comprehensive and easy to use, while struts2 needs to use a lot of XML configuration parameters to manage (although annotations can also be used, but almost no companies use them).

4. Parameter passing: Struts2 itself provides a variety of parameters to accept, in fact, are passed and assigned through (ValueStack), and SpringMvc is received through the parameters of the method.

5. Learning difficulty: Struts has many new technical points, such as interceptors, value stacks and OGNL expressions. The learning cost is high. Springmvc is relatively simple and can be used in very little time.

6. Intercepter implementation mechanism: struts has its own interceptor mechanism, and spring mvc uses an independent AOP method. In this way, the amount of configuration files of struts is still larger than that of spring mvc, although the configuration of struts can be inherited, so I think that in terms of use, spring mvc is more concise, and the development efficiency of spring mvc is indeed higher than that of struts2. Spring mvc is a method-level interception, a method corresponds to a request context, and a method corresponds to a url at the same time, so spring3 mvc can easily implement restful url from the architecture itself. Struts2 is a class-level interception, a class corresponds to a request context; it takes effort to implement a restful url, because a method of struts2 action can correspond to a url; and its class attributes are shared by all methods, which cannot use annotations or other methods. Identifies the method to which it belongs. The methods of spring3 mvc are basically independent, and the request response data is exclusively used. The request data is obtained through parameters, and the processing results are returned to the framework through ModelMap. Variables are not shared between methods, while struts2 is messy, although between methods It is also independent, but all its Action variables are shared, which will not affect the operation of the program, but it will bring trouble to us when coding and reading the program.

7. Spring mvc processes ajax requests directly by returning data, using the annotation @ResponseBody in the method, and spring mvc automatically converts our objects to JSON data.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327072050&siteId=291194637