The difference between Struts2 and spring MVC

Source: Internet
Author: User

Overview:

Struts2Framework,Actionclass is its core class,xxxactionis called the business logic controller, realizes the processing to the user request information. Struts2introduction of the framework, usingxxxactionas a control layer, the view layer is separated from the model layer, which truly implements theMVCdevelopment model, making the whole project intoModel (Persistence Layer/Business Layer),View,Controllerthree layer, which makes the software coupling degree is reduced.

Springmvcand theStruts2the function of the framework is similar, and the separation between the view layer and the control layer is realized, and the software coupling degree is reduced. And,Springmvcis aSpringbuilt -inSpring WebFramework, developers can freely choose their own projects to integrate theSpring Webframework that can be used inSpringintegrated inSpringbuilt-inSpring WebFrameSpring MVC, you can alsoSpringintegration of third partiesWebframework such asStruts2.

Summary: Struts2and theSpring MVC's AllWebframeworks, the purpose of which they are introduced in engineering is to separate the view layer from the control layer, making the code further decoupled. Only the developers of these two open source projects are different, so there will be a slight difference in the working mechanism. Spring MVCis aSpringProject's built-inWebFramework, whileStruts2Then andSpringthe organization does not have a direct connection, and the developer should choose to do their own projectsSpring MVCor should I chooseStruts2, this will be based on the actual project that the developer is responsible for choosing. (Springnot only supports its built-inWebFrameSpring MVC, it also supports three-partyWebframework such asStruts2, which makes the developer's Choice more flexible)

Difference:

1)both Struts2 and Springmvc are Method-level interception, and a method corresponds to a request context. However, they are implemented in different ways:

SPRINGMVC uses annotation @requestmapping () to implement method-level interception, a method that corresponds to a request context.

However, by default Struts2 is class-level interception, and a xxxaction class corresponds to a request context, and when a request arrives, the Execute () method corresponding to the Xxxaction class is executed by default. If you want Struts2 to be a method-level intercept, where different requests correspond to different methods in the class, you need to configure the <action> element's method property in the Struts2 configuration file Struts.xml to actually Now a request corresponds to a method in the class, which enables method-level interception.

2) different data sharing methods for Struts2 and Springmvc

The STRUTS2 framework is class-level blocking, creating an action each time a request is made, and then invoking the setter getter method to inject the data from the request. Struts2 is actually dealing with the request through setter getter methods. Its class properties are shared by all methods, although the methods are also independent, but all of their action variables are shared, which does not affect the program's operation, but gives us trouble coding the program.

Unlike Spring3 MVC, SPRING3MVC is a method-level intercept that intercepts the method and injects the request data into it based on annotations on the parameters. The SPRING3MVC approach is essentially independent, and the request response data requests data is obtained through parameters, and the results are returned to the framework via MODELMAP. The variables are not shared between methods.

3) different interception mechanisms for Struts2 and Springmvc

STRUTS2 can be configured with a configuration file for Xxxaction to configure a number of interceptors, through these interceptors to achieve permission control, request preprocessing (such as Check request/correction request header or request parameter data), Response remediation ( such as modifying response headers and response data).

Struts has its own interceptor mechanism, and spring MVC uses an independent AOP approach.

4) different implementation mechanisms of Struts2 and Springmvc

The entrance to Spring MVC is the servlet, and struts is the filter (it is noted here that the filter and servlet are different.) You can think of filter as a special kind of servlet, a servlet chain)

5) It is cumbersome to write a configuration file when developing with struts+spring, but it is not so troublesome to use the Spring mvc+spring to use it in a direct way. Spring MVC can eliminate the hassle of writing configuration files

We used struts2 when using the traditional configuration file in a way that did not use the legendary 0 configuration. SPRING3 MVC can be thought of as having 100% 0 configured (in addition to configuring spring Mvc-servlet.xml).

6) due to struts2 need for each request to encapsulate, the request session servlet Life cycle variables are encapsulated into a single map action use and ensure thread safety, so in principle, it is more memory-intensive.

7) Springmvc integrated Ajax, easy to use, just one annotation @ResponseBody can be implemented, and then directly return the response text, and Struts2 The Interceptor integrates Ajax , in Action in the processing of the general must install plug-ins or their own code integration into the use is relatively inconvenient.

8) SPRINGMVC Verification Support JSR303, processing relatively more flexible and convenient, and Struts2 Verification is more cumbersome, feel too upset.

9) Spring MVC and spring are seamless. The management and security of this project is also higher than Struts2 (of course Struts2 can also be achieved through different directory structures and related configurations Springmvc the same effect, but requires XML configured in a few places).

10) design ideas,Struts2 more in line with OOP programming ideas, springmvc is more cautious, in extension on the servlet.

SPRINGMVC Development efficiency and performance is higher than Struts2.

More differences between Struts2 and springmvc can be found in the following materials

http://blog.csdn.net/gstormspire/article/details/8239182

Http://www.tuicool.com/articles/AneA3yr

The difference between Struts2 and spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.