Similarities and differences between SPRINGMVC and struts

Source: Internet
Author: User
Tags expression engine

Spring MVC is a follow-on 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 the spring pluggable MVC architecture, you can choose to use a built-in spring web framework or a WEB framework such as Struts. With the policy interface, the Spring framework is highly configurable and includes a variety of view technologies, such as JavaServer Pages (JSP) technology, Velocity, Tiles, IText, and POI. The Spring MVC framework does not know which view to use, so it does not force you to use only JSP technology. Spring MVC separates the roles of controllers, model objects, dispatchers, and handler objects, and this separation makes them easier to customize.

Here are some of the things that I learned about SPRINGMVC.

Put this picture here, I want to say Springmvc and Struts2 really is not the same, although in all have the core distributor , such as the same functional components (these are determined by the MVC pattern itself).

Why would SPRINGMVC win the final victory? Talk about some of my own views:

First, the MVC framework appears to map URLs from the world of HTTP to the Java world, which is the core function of the MVC framework. And at the URL this point Springmvc undoubtedly more elegant.

Second, from the perspective of design, I think springmvc more clear. Even if we were to compare Struts2 's schematic with Springmvc's class diagram, it was still confusing, far from Springmvc more intuitive:

SPRINGMVC design ideas: Standardize the entire process and assign each process step to a different component for processing.

This scenario actually involves two areas:

L Processing Process Normalization-divides the processing process into several steps (tasks) and concatenates all the steps using a clear logical main line

L Process component-defines each step (task) in the processing process as an interface and assigns different implementation patterns to each interface

The process normalization is the purpose, and the procedure and process definition of the process are the means. Thus, the primary content of process normalization is to consider the logical steps that a generic servlet responder should roughly include:

L step to the HTTP request for preliminary processing, find the corresponding Controller processing class (method)--handlermapping

L Step 2--Call the appropriate Controller processing class (method) to complete the business logic--handleradapter

L Step 3--Handle exceptions that can occur when a controller processes a class (method) Call--handlerexceptionresolver

L Step 4--HTTP Response processing based on the call result of the Controller processing class (method)--viewresolver

It is this design based on component and interface that supports another feature of SPRINGMVC: the extensibility of behavior.

Thirdly, the design principle is more clear.

"Open for extension/closed for modification"

This important design principle is written in the starting section of the SPRINGMVC chapter in the official Spring reference:

[HTML]View Plaincopyprint?
    1. A key design principle in Springweb MVC and in Spring in general are the "Open for extension, closed for modification" princ Iple.

And the emphasis is well embodied in the implementation of the SPRINGMVC, can be expanded, but cannot be changed. I have extended the spring IOC, AOP features, which should be SPRINGMVC with spring same strain.

Finally, the modular design and the specific design principles let the springmvc-shaped scattered god gather.
    • God--springmvc always runs along a fixed logical line.
    • --SPRINGMVC has many different behavioral patterns.

SPRINGMVC is a component-based development framework in which different implementations of components form a "form", and the logical concatenation of components constitutes "God". Therefore, the "scattered God does not disperse": SPRINGMVC's logical Main line is always the same, but the behavior pattern can be varied.

Five, more closely conform to the trend of web development, this is more virtual, no longer talk about this problem.

The technical slowdown resulted in the programmer losing enthusiasm for Struts2, which led Springmvc to rely on his own efforts and spring's reputation, gradually revealing its own advantages and characteristics.

Why would SPRINGMVC win the final victory?

Finally, we might as well think about how Struts2 is popular!

I used to come from the Struts1, and later Struts1 the problem is obvious, the open source community appeared a lot of MVC framework, the most prominent is WEBWORK2.

WEBWORK2 explored a different solution from the traditional servlet model, and was gradually known and understood, and developed and recognized by the majority of programmers. With excellent design ideas and flexible implementation, it attracts a large number of web-layer developers into its embrace.

The Apache community and Opensymphony announced that the future Struts project will be merged with the WEBWORK2 Project and launched jointly with STRUTS2.

STRUTS2 's ability to dominate the development market over a long period of time is due to its technological leadership. The leading edge of this technology is highlighted by a thorough transformation of the controller:

[Java]View Plaincopyprint?
    1. Public class Usercontroller {
    2. Private User User
    3. Public String Execute () {
    4. //Add business logic code here
    5. return "Success";
    6. }
    7. }



From the code above, we can see that Webwork2/struts2 has two points for the controller's biggest retrofit:

    • The introduction of native servlet objects such as HttpServletRequest or HttpServletResponse is completely eliminated in the controller.
    • Both the request parameter and the response data are stripped from the response method to the property variables in the controller.

These two major transformation is regarded as the genius of the frame. Because of this transformation, the entire controller class is completely decoupled from the Web container and can be easily carried out in unit testing. And get rid of the servlet bound controller, but also for the entire programming model to give a new definition. From the perspective of introducing new programming elements, WEBWORK2/STRUTS2 is undoubtedly a success. Because the attribute variables in the off-limits controller in the traditional servlet pattern are reasonably utilized as part of the data in the request processing process. This transformation not only allows the expression engine to be maximized, but also makes the entire controller look more like a pojo.

Thus, this form of expression is named by the Author: Pojo implementation mode. The Pojo implementation pattern is a revolutionary pattern because it is able to maximize the idea of decoupling. From an object-oriented perspective, the Pojo pattern is undoubtedly one of the goals that all programmers pursue. This is one of the important reasons why Webwork2/struts2 has been enduring for so many years.

So, the first reason we see this is that STRUTS2 relies on technological innovation to win the favor of programmers. But the Struts2 of technological innovation over the years seems to have taken a smaller stride. We can see that after the popularization of JDK1.5, annotation as a new Java syntax, is gradually known and applied. This point Springmvc followed the trend of the times, directly for the request-response mapping. But Struts2 has been unable to form a breakthrough in the single configuration source. Of course, this is just a simple example of technological innovation, and there are many other examples.

At least that's the way it feels to people. Struts is not very dipping at this point, as spring's word of mouth and influence also objectively deepens everyone's impression of SPIRNGMVC as a technology leader.

Difference of contact

1. Implementation Mechanism

The STRUTS2 framework is class-level blocking, creating a corresponding action in the controller 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. In Struts2, an Action object corresponds to a request context.

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. In Spring3mvc, a method corresponds to a request context, and the method corresponds to a URL at the same time. So it's easy to implement restful URLs spring3 MVC from the architecture itself. The STRUTS2 architecture is hard to implement because one method of struts2 action can correspond to a URL, and its class properties are shared by all methods, which means that it cannot be identified by annotations or other means.

The entry for spring MVC is the servlet, and Struts2 is the filter (it is noted here that the filter and servlet are different)

Spring MVC will be slightly faster than struts.

2. Request data sharing

Spring3mvc's approach is essentially independent, and the request response data is exclusive. The request data is obtained through the parameters, and the processing results are returned to the framework via MODELMAP, and the variables are not shared between methods. and Struts2, although the method is also independent, but all of its action variables are shared. This does not affect the program's operation, but it gives us trouble coding and reading the program.

3. Parameter Transmission

Struts is a parameter that can be accepted with attributes when it accepts parameters, which means that the parameters are shared by multiple methods.

4. Design Ideology

Struts is more consistent with OOP programming ideas, and spring is more cautious and extensible on the servlet.

5. Implementation mechanism of Intercepter

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, the validation of Spring3 MVC is also a bright spot, supporting JSR303

Ajax requests are more convenient, just one annotation @responsebody, then directly return the response text. Send the following code:

[Java]View Plaincopyprint?
  1. @RequestMapping (value="/whitelists")
  2. Public String index (Modelmap map) {
  3. Account account = Accountmanager.getbydigitid (Securitycontextholder.get (). Getdigitid ());
  4. list<group> 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. }


Next case Analysis Springmvc Mystery, please look forward to!

Similarities and differences between SPRINGMVC and struts

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.