Difference between Struts2 and Spring mvc

Source: Internet
Author: User

First paste two flowcharts and then analyze them one by one:

Struts2 MVC Framework Spring MVC Framework

Spring MVCPKStruts2

We use the traditional configuration file method when using struts2, and do not use the legendary 0 configuration. Spring3 mvc can be considered to have 100% Zero Configuration (except for configuring spring mvc-servlet.xml ).

Differences Between Spring MVC and Struts2:

1. mechanism: the entry of spring mvc is servlet, while struts2 is filter (it should be noted that the filter and servlet are different. I used to think that filter is a special type of servlet), which leads to different mechanisms between the two. The difference between servlet and filter is involved here.

2. Performance: spring is slightly faster than struts. Spring mvc is a method-based design, while sturts is a class-based design. Each request is sent with an action instance, and each action is injected with attributes. spring is based on methods with finer granularity, but be careful with the data as controlled in servlet. Spring3 mvc is a method-level interception. After the method is intercepted, the request data is injected according to the Annotation on the parameter. In spring3 mvc, a method corresponds to a request context. The struts2 framework is a Class-level interception. When a request comes, an Action is created, and the setter getter method is called to inject the data in the request; struts2 is actually dealing 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 using attributes when accepting parameters. This means that parameters are shared by multiple methods.

4. design ideology: struts is more in line with the oop programming ideology, so spring is more cautious and can be expanded on servlet.

5. intercepter Implementation Mechanism: struts has its own interceptor mechanism, and spring mvc uses an independent AOP method. As a result, the number of struts configuration files is larger than that of spring mvc. Although struts configurations can be inherited, I think spring mvc is more concise in terms of usage, development efficiency Spring MVC is indeed higher than struts2. Spring mvc is a method-level interception. A method corresponds to a request context, and the method corresponds to a url at the same time. Therefore, spring3 mvc can easily implement restful URLs in the architecture itself. Struts2 is a Class-level interception. A Class corresponds to a request context. It is difficult to implement a restful url, because a method of struts2 action can correspond to a url, while its class attributes are shared by all methods, in this way, the method to which the object belongs cannot be identified using annotations or other methods. Spring3 mvc methods are basically independent, with exclusive request response data. The request data is obtained through parameters, and the processing results are returned to framework methods through ModelMap without sharing variables, struts2 is messy. Although the methods are independent, all their Action variables are shared, which does not affect the program running, but it gives us coding and troubles when reading the program.

6. In addition, spring3 mvc verification is also a highlight. JSR303 is supported. It is more convenient to process ajax requests. You only need an annotation @ ResponseBody and then directly return the response text. Send a piece of code:

@ RequestMapping (value = "/whitelists ")
Public String index (ModelMap map ){
Account account = accountManager. getByDigitId (SecurityContextHolder. get (). getDigitId ());
List GroupList = groupManager. findAllGroup (account. getId ());
Map. put ("account", account );
Map. put ("groupList", groupList );
Return "/group-index ";
}

// @ ResponseBody ajax response, which is convenient for processing Ajax requests
@ RequestMapping (value = "/whitelist/{whiteListId}/del ")
@ ResponseBody
Public String delete (@ PathVariable Integer whiteListId ){
WhiteListManager. deleteWhiteList (whiteListId );
Return "success ";
}


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.