Comparison between SPRINGMVC and Struts2

Source: Internet
Author: User

1. STRUTS2 is class-level interception, a class corresponds to a request context, SPRINGMVC is a method-level intercept, a method corresponds to a request context, and the method corresponds to a URL, So it's easy to implement restful URLs from the architecture itself, and Struts2 's architecture is SPRINGMVC because a method of action in Struts2 can correspond to a URL, and its class properties are shared by all methods. It is also not possible to identify the method that it belongs to by annotations or other means.

2, from the above reasons, Springmvc method is basically independent of the request response data, requests for data through parameters, processing results passed modelmap to the framework, the method does not share variables, And Struts2 do is more chaotic, although the method is also independent, but all of its action variables are shared, this will not affect the program to run, but give us the code to read the program caused trouble, each time a request to create an action, An Action object corresponds to a request context.
3, since Struts2 needs to be encapsulated for each request, the variables of the servlet life cycle such as request,session are encapsulated into a map, supplied for each action, and thread-safe, so in principle, is more memory-intensive.

4, The interceptor implementation mechanism, STRUTS2 has its own interceptor mechanism, SPRINGMVC with the independent AOP way, so that Struts2 configuration file volume is still larger than SPRINGMVC.

5. The SPRINGMVC entrance is the servlet, and Struts2 is the filter (it is noted here that the filter and servlet are different.) It was thought that filter was a special kind of servlet, which led to a different mechanism between the two, which involved the difference between servlet and filter.

6. SPRINGMVC integrates Ajax, is very convenient to use, just one annotation @responsebody can be implemented, and then directly return to the response text, and STRUTS2 interceptor integrated Ajax, In action, it is generally necessary to install plug-ins or write code to integrate them, it is relatively inconvenient to use.

7. SPRINGMVC verification support JSR303, processing relatively more flexible and convenient, and Struts2 verification is more cumbersome, feel too upset.

8. Spring MVC and Spring are seamless. From this project management and security is also higher than Struts2 (of course Struts2 can also be done through different directory structure and related configuration to achieve springmvc the same effect, but need XML configuration of a lot of places).

9, design ideas, Struts2 more in line with OOP programming ideas, SPRINGMVC more cautious, extended on the servlet.

10. SPRINGMVC development efficiency and performance is higher than Struts2.
11. SPRINGMVC can be considered already 100% 0 configured.

Spring and its advantages

Most of the projects are not the spring figure, why everyone is so popular with him, and his pursuit of the slightest decline in the trend

What Spring is:

Spring is a lightweight Di and AOP container framework.

A large part of the reason that it is lightweight is relative to EJB (although I have never been exposed to EJB applications), it is important that spring is non-intrusive and spring-based applications generally do not rely on spring classes.

 

DI: called Dependency injection (Dependency injection), and control reversal a concept, specifically, When a role needs to be assisted by another role, in a traditional program design, there is usually a caller who creates an instance of the callee. But creating the callee in spring will no longer have callers complete, so call control inversion. The creation of the called object has spring to complete, and when the container instantiates the object it actively injects the callee (or its dependent object) to the calling object, and therefore is called dependency injection.

AOP: Spring provides strong support for aspect-oriented programming by allowing us to separate business logic from application services (such as transaction management) for cohesion clustering, where application objects focus only on business logic, No longer responsible for other system issues (such as logs, transactions, etc.). Spring supports user-defined facets. The

aspect-oriented programming is a powerful complement to object-oriented programming. Object-oriented programming divides a program into objects of all levels, and a tangent-oriented program decomposes the running process into facets. AOP is to consider the structure of the program from the point of view of running the program, extract the aspect of the business process, OOP is static abstraction, AOP is the dynamic abstraction, it is the procedure of application execution process abstraction, thus obtains the logical division between steps.

Container: Spring is a container because it contains and manages the life cycle and configuration of the Application object. such as object creation, destruction, callback, and so on.

framework: Spring, as a framework, provides some basic functionality, such as transaction management, persistence layer integration, and so on, so that developers are more focused on developing application logic.

See what spring is and see what spring does

1. Using spring's IOC container, the dependencies between the objects are given to spring, and the coupling between the components is reduced, allowing us to focus more on the application logic

2. Can provide many services, transaction management, WS and so on.

3.AOP is very good support, instant noodles programming to the plane.

4. The mainstream framework provides good integration support, such as HIBERNATE,STRUTS2,JPA

The 5.Spring di mechanism reduces the complexity of business object substitution.

6.Spring is a low-intrusion, very low code pollution.

The 7.Spring is highly open and does not force reliance on spring, and developers are free to select part or all of spring

Advantages of STRUTS2
Struts2 is a fairly powerful Java Web Open source Framework, an MVC web framework based on the Pojo action. Based on the webwork and xwork frameworks of the year, it inherits its merits and makes considerable improvements. Struts2 now in the Java Web Development community can be said to be rounds, from the developer's point of view, Struts2 can be so deep in the development of the people's heart, and its excellent design is inseparable.


1, Struts2 based on the MVC architecture, the frame structure is clear, the development process at a glance, developers can well control the development process.
In
my project development process, a specific function of the development process is: to get a specific functional requirements of the document and the design of the foreground interface (in development I am not responsible for designing the page), analysis needs to pass from the foreground which parameters, determine the parameters of the variable name, set the appropriate variables in the action, These parameters are displayed in the foreground, and some of the controls on the page are appropriately replaced with the server-side controls provided by STRUTS2, writing action-corresponding methods to complete the business logic, and finally, doing some configuration file-related settings. Of course, the actual development is more complex than this process, involving database, validation, exception processing. But with Struts2, the vast majority of your focus is on how to implement business logic, and the development process is clear and straightforward.
2, using OGNL for parameter transfer. OGNL provides an easy way to access data from various scopes in the STRUTS2, and you can easily get data from the request,attribute,application,session,parameters. Greatly simplifies the amount of code the developer gets when the data is fetched.
3, the powerful interceptor STRUTS2 Interceptor is an action-level aop,struts2 in many of the features are implemented through interceptors, such as exception handling, file upload, verification and so on. Interceptors are configurable and reusable, and some common features such as login verification, permission validation, etc. are placed in interceptors to complete some of the more common features in Java Web projects. In my implementation of a Web project, is the use of STRUTS2 interceptor to complete the system of authorization authentication function. 4, easy to test Struts2 action are simple pojo, so that the Struts2 action can be easily written test cases, greatly facilitate the Java Web project testing. 5, Easy to expand the plug-in mechanism in Struts2 add extension is a pleasant and easy thing, just need to put the required jar package into the Web-inf/lib folder, in Struts.xml to make some simple settings can be extended. The commonly used STRUTS2 extensions can be found through this link: http://cwiki.apache.org/S2PLUGINS/home.html 6, modular Struts2 has made modularity the basic idea in architecture, There are three ways to modularize your application: Split configuration information into multiple files create a new framework feature for your self-contained application module as a plug-in, and new features that are not relevant to a particular application are organized into plugins to add to multiple applications. 7. Global results and declarative Exceptions add global result to the application and handle exceptions in the configuration file so that when a specified exception occurs during processing, it is useful to jump to a specific page.

The advantages of Spring MVC and STRUTS2 comparison

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).

The difference between Spring MVC and STRUTS2:

  1. mechanism:The entry for spring MVC is the servlet, and Struts2 is the filter (it is noted here that the filter and servlet are different.) Previously thought that filter was a special type of servlet, which led to a different mechanism between the two, which involved the difference between servlet and filter.

  2. performance:spring will be slightly faster than struts. Spring MVC is a method-based design, and Sturts is class-based, each time a request is made with an action, each action is injected into the attribute, and spring is based on a method that is finer grained, but be careful to hold the same data as the servlet. SPRING3 MVC is a method-level interception that, after intercepting a method, injects the request data into a parameter based on annotations, and in Spring3 MVC, a method corresponds to a request context. The STRUTS2 framework is a class-level intercept, creating an action every time a request is made, and then invoking the setter getter method to inject the data from the request; Struts2 actually deals with the request through the setter getter method. ; in Struts2, an Action object corresponds to a request context.

  3. parameter passing:struts can accept parameters with attributes when they are accepted, which means that the parameters are shared by multiple methods.

  4. Design thinking:struts more in line with OOP's programming ideas, spring is more cautious, extended on the servlet.

  5. intercepter Implementation mechanism:struts has its own interceptor mechanism, and spring MVC uses an independent AOP approach. This leads to the profile of struts is still larger than spring MVC, although the configuration of struts can inherit, so I think in terms of use, Spring MVC use more concise, development efficiency Spring MVC is really higher than struts2. Spring MVC is a method-level intercept, a method that corresponds to a request context, and a method that corresponds to a URL, so it is easy to implement a restful URL spring3 mvc from the schema itself. STRUTS2 is class-level interception, a class corresponds to a request context, and implementing a restful URL is laborious because a method of struts2 action can correspond to a URL, and its class properties are shared by all methods. It is also not possible to identify the method that it belongs to by annotations or other means. Spring3 MVC method is basically independent, the request response data, requests data through parameters, processing results through the MODELMAP to the framework method is not shared between the variables, and struts2 make is more chaotic, although the method is also independent, But all of its action variables are shared, which does not affect the program's operation, but it gives us code and trouble reading the program.

  6. In addition,spring3 MVC validation is also a bright spot, support JSR303, processing AJAX Requests is more convenient, just a note @responsebody, and then directly return the response text. Send the following code:

    [Java]View PlainCopy  
    1. @RequestMapping (value= "/whitelists")
    2. Public String index (Modelmap map) {
    3. Account account = Accountmanager.getbydigitid (Securitycontextholder.get (). Getdigitid ());
    4. List grouplist = Groupmanager.findallgroup (Account.getid ());
    5. Map.put ("account", account);
    6. Map.put ("Grouplist", grouplist);
    7. return "/group/group-index";
    8. }
    9. @ResponseBody Ajax response, it's also easy to handle AJAX requests
    10. @RequestMapping (value= "/whitelist/{whitelistid}/del")
    11. @ResponseBody
    12. Public String Delete (@PathVariable Integer whitelistid) {
    13. Whitelistmanager.deletewhitelist (Whitelistid);
    14. Return "Success";
    15. }

Comparison between SPRINGMVC and Struts2

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.