SpringMVC series (ii) differences between springMVC and Struts
Spring MVC is a follow-up product of SpringFrameWork and has been integrated into Spring Web Flow. The Spring framework provides a full-featured MVC module for Building Web applications. Using Spring-pluggable MVC Architecture, you can choose to use a built-in Spring Web framework or a Web framework such as Struts. Through the policy interface, the Spring framework is highly configurable and contains multiple view technologies, such as assumerver Pages (JSP), Velocity, Tiles, iText, and POI. The Spring MVC Framework does not know the view used, so it does not force you to only use JSP technology. Spring MVC separates the roles of controllers, model objects, schedulers, and processing program objects, making them easier to customize.
The following content shows how I learned spring MVC.
Here, I want to say that SpringMVC and Struts2 are really different.Core distributorAnd other functional components (these are determined by the MVC mode itself ).
Why did SpringMVC win the final victory? My own opinions are as follows:
First, the emergence of the MVC framework is to map URLs from the HTTP world to the JAVA World, which is the core function of the MVC framework. SpringMVC is undoubtedly more elegant in terms of URL.
Second, from the perspective of design implementation, I think SpringMVC is clearer. Even if we compare the Struts2 schematic diagram with the SpringMVC class diagram, it is still confusing, far from being SpringMVC more intuitive:
SpringMVC Design Concept: standardizes the entire processing process and assigns each processing step to different components for processing.
This solution actually involves two aspects:
L process normalization-divide the process into several steps (tasks), and use a clear logical main line to connect all the steps
L process componentization-define every step (task) in the process as an interface and assign different implementation modes to each interface
Process standardization is the purpose, and step division and process definition are the means for the process. Therefore, the first thing to standardize the processing process is to consider the logical steps that a general Servlet Response Program should include:
L step 1 -- perform preliminary processing on the Http request and find the corresponding Controller processing class (method) -- HandlerMapping
L step 2 -- call the corresponding Controller processing class (method) to complete the business logic -- HandlerAdapter
L step 3 -- handle exceptions that may occur during Controller processing class (method) calls -- HandlerExceptionResolver
L step 4 -- perform Http Response Processing Based on the call results of the Controller processing class (method) -- ViewResolver
This design is based on components and interfaces and supports another feature of SpringMVC: behavior scalability.
Third, the design principles are clearer.
[Open for extension/closed for modification]
This important design principle is written in the starting section of SpringMVC in Spring official reference:
A key design principle in SpringWeb MVC and in Spring in general is the“Open for extension, closed for modification” principle.
In addition, the focus is well reflected in SpringMVC implementation, which can be expanded, but cannot be changed. I have extended Spring's IOC and AOP functions. SpringMVC should be in the same line with Spring.
Fourth, the componentized design scheme and specific design principles let SpringMVC form a distraction.
God-SpringMVC always runs along a fixed logic Main Line
Shape-SpringMVC has a variety of different behavior patterns
SpringMVC is a component-based development framework. Different implementation systems of components constitute a "shape". The logical concatenation of components constitutes a "God ". Therefore, the logic Main Line of SpringMVC remains unchanged, but the behavior patterns can be varied.
Fifth, it is better to adapt to the development trend of Web. This is even more virtual, and we will not talk about this issue any more.
Sixth, the technical slowdown caused programmers to lose enthusiasm for Struts2. SpringMVC gradually revealed its own advantages and characteristics by relying on its own efforts and the reputation of Spring.
Why did SpringMVC win the final victory?
Finally, let's think about how Struts2 is popular!
I used it from Struts1. Later, the problem with Struts1 was obvious. Many MVC frameworks emerged in the open-source community, most prominent of which:Webwork2.
Webwork2 has explored a solution different from the traditional Servlet model and is gradually recognized and understood by developers. With its excellent design ideas and flexible implementation, it attracts a large number of Web-layer developers to invest in it.
The Apache community and Opensymphony announce that the future Struts project will be merged with the Webwork2 project and Struts2 will be jointly launched.
The major reason why Struts2 can take the dominant position in the development market for a long period of time lies in its technological advantages. This technological advantage highlights the complete transformation of the Controller:
Public class UserController {private User user public String execute () {// Add the business logic code return "success ";}}
From the code above, we can see that Webwork2/Struts2 has two major improvements to Controller:
Completely eliminate the introduction of native Servlet objects such as HttpServletRequest or HttpServletResponse In the Controller. The Request Parameters and response data are stripped from the response method to the attribute variables in the Controller.
These two major transformations are seen as the magic of the framework. With this transformation, the entire Controller class is completely decoupled from the Web Container, allowing for easy unit testing. The Controller, which is free from Servlet limitations, also gives a new definition to the entire programming model. From the perspective of introducing new programming elements, Webwork2/Struts2 is undoubtedly successful. In the traditional Servlet mode, attribute variables in the remote Controller are reasonably used as the data part in the request processing process. This transformation not only maximizes the expression engine, but also makes the entire Controller look more like a POJO.
Therefore, this form is named by the author: POJO implementation mode. The POJO implementation mode is a revolutionary mode because it can bring the idea of decoupling to the extreme. From an object-oriented perspective, the POJO model is undoubtedly a goal pursued by all programmers. This is an important reason why Webwork2/Struts2 has been enduring for so many years.
Therefore, we can see that the first reason is that Struts2 has won the favor of programmers by relying on technological innovation. However, in the past few years, Struts2's role in technological innovation seems to be relatively small. As we can see, after the popularity of JDK1.5, Annotation, as a new Java syntax, is gradually known and applied. SpringMVC follows the trend of the times and is directly used for request-response ing. However, Struts2 has been unable to break through the problem of a single configuration source. Of course, this is just a simple example of technological innovation. There are many other examples.
At least it gives people the feeling. At this point, Struts is not very good, because the reputation and influence of Spring have also become more objective, which makes SpirngMVC a technology leader.
Difference
1. Implementation Mechanism
The struts2 framework is a Class-level interception. Each time a request is sent, a corresponding Action in the controller is created, and the setter getter method is called to inject data in the request. Struts2 actually deals with requests through the setter getter method. In struts2, an Action object corresponds to a request context.
Spring3 mvc is different. spring3mvc is a method-level interception. After the method is intercepted, the request data is injected according to the Annotation on the parameter. In spring3mvc, a method corresponds to a request context, and the method also corresponds to a url. Therefore, spring3 mvc can easily implement restful URLs in the architecture itself. The implementation of struts2 architecture is difficult, because a method of struts2 action can correspond to a url, and 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.
The entry of spring mvc is servlet, while struts2 is filter (the filter and servlet are different here)
Spring MVC is slightly faster than struts.
2. Request data sharing
Spring3mvc methods are basically independent and exclusive to request response data. Request data is obtained through parameters, and the processing result is returned to the framework through ModelMap. variables are not shared between methods. Struts2 is messy. Although methods are independent, all the Action variables are shared. This will not affect the program running, but will cause us trouble in coding and reading the program.
3. PASS Parameters
Struts can use attributes to accept parameters when accepting parameters. This means that parameters are shared by multiple methods.
4. design ideology
Struts is more in line with oop programming ideas, so spring is more cautious and can be expanded on servlet.
5. Implementation Mechanism of intercepter
Struts has its own interceptor mechanism. 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, supporting JSR303
Ajax requests are more convenient. 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 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, it is also convenient to process Ajax requests @ RequestMapping (value = "/whitelist/{whiteListId}/del") @ ResponseBodypublic String delete (@ PathVariable Integer whiteListId) {whiteListManager. deleteWhiteList (whiteListId); return "success ";}
Next, let's analyze springMVC's mysterious veil. Please look forward to it!