spring mvc and Struts2

Although I have not systematically learned the Spring MVC framework, but I have been working for so long, basically all Spring MVC is used in the WEB layer. I think Struts2 is also a good WEB layer framework. The two frameworks have not yet been compared. , I have nothing to do today, I found some information from the Internet, plus my usual experience of using Spring MVC to summarize.

Spring MVC PK Struts2



We use the traditional configuration file method of 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).
The difference between Spring MVC and Struts2:

1. Mechanism: The entrance of spring mvc 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 kind of servlet), which led to two The mechanism of the user is different, and the difference between servlet and filter is involved here.

2. Performance: spring will be slightly faster than struts. Spring mvc is a method-based design, while sturts is class-based. Every time a request is made, an action will be instantiated, and each action will be injected with attributes, while spring is method-based and has a finer granularity, but be careful to control data like in servlets Same. Spring3 mvc is a method-level interception. After intercepting the method, the request data is injected into it according to the annotations on the parameters. In spring3 mvc, a method corresponds to a request context. The struts2 framework is a class-level interception. Every time a request comes, an Action is created, and then the setter getter method is called to inject the data in 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 use attributes to accept parameters when accepting parameters, which means that parameters are shared by multiple methods.

4. Design thinking: struts is more in line with oop's programming thinking, spring is more cautious and expands on servlet.

5. 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 shared. The request data is obtained through parameters, and the processing results are returned to the framework through ModelMap. Variables are not shared between the methods, while struts2 is messy, although the 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.

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

@RequestMapping(value="/whitelists")
public String index(ModelMap map) {
Account account = accountManager.getByDigitId(SecurityContextHolder.get().getDigitId());
List<Group> groupList = groupManager.findAllGroup(account.getId());
map.put("account", account);
map.put("groupList", groupList);
return "/group/group-index";
}

// @ResponseBody ajax响应,处理Ajax请求也很方便
@RequestMapping(value="/whitelist/{whiteListId}/del")
@ResponseBody
public String delete(@PathVariable Integer whiteListId) {
whiteListManager.deleteWhiteList(whiteListId);
return "success";
}

Guess you like

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