The difference between Struts1 and struts2 _ Power node OF Java College Collation

  • 2020-09-28 08:53:32
  • OfStack

In fact, Struts2 is not a strange Web framework. Struts2 takes the design idea of Webwork as the core and absorbs the advantages of Struts1. Therefore, Struts2 can be considered as the product of the combination of Struts1 and Webwork.

In short, the difference between the two is as follows:

One is Stuts1 and the other is Stuts2, which is the biggest difference. In terms of technology, Stuts1 has a core controller, but it only provides one interface, that is execute. It is very troublesome to configure actionform and so on, so the dependency is strong. Stuts2, developed for interceptors, is the so-called AOP idea. It can be configured with multiple action, which makes it easier to use, but because the interceptor before the request has some injection operation, it is a bit slower than Stuts1.

1. MVC profile

Struts2 is an MVC framework that is compatible with Struts1 and WebWork. Since the MVC framework is mentioned, we have to make a brief introduction of MVC framework, which is limited to a brief introduction. If you want to know more about MVC, you can check relevant documents or find a book of Struts1, I believe there will not be too little space on MVC. To get back to the point, the ultimate purpose of the frameworks that are currently emerging from Java is contact coupling, whether Spring, Hibernate, or MVC, for increasing reuse. MVC contacts the coupling between View and Model.

MVC consists of three basic parts: Model, View, and Controller, which work together with minimal coupling to increase the scalability and maintainability of the program. The implementation techniques of each part can be summarized as follows:

1) Model: JavaBean and EJB
2) View: JSP, Struts TagLib
3) Controller: ActionServlet and Action of Struts

To sum up, the advantages of MVC mainly include the following aspects:

1) Multiple views can correspond to one model. According to MVC design mode, one model corresponds to multiple views, which can reduce code copy and code maintenance. Once the model changes, it is also easy to maintain
2) The data returned by the model is separated from the display logic. The model data can be applied using any display technology, for example, using JSP pages, Velocity templates, or directly generating Excel documents
3) The application is divided into three layers, which reduces the coupling between each layer and provides the scalability of the application
4) The concept of control layer is also effective because it combines different models and different views in one to complete different requests. Therefore, the control layer can be said to contain the concept of user request permissions
5) MVC is more in line with the spirit of software engineering management. The different layers do their job, and each layer's components have the same characteristics that facilitate the generation of hypervisor code through engineering and tooling

So much for the conceptual stuff of MVC, where the key is what the implementation technology is for each module.

2. Struts2 profile

Developed since Struts2 Struts1, but actually Struts2 and Struts1 above the design idea of the framework or there is a big difference, Struts2 is WebWork design as the core, why not use Struts1 Struts2 design idea, after all Struts1 in the present enterprise application is a very big market in, so, see Struts1 1 some shortcomings:

1) Supported presentation layer technology Sheet 1
2) Heavily coupled to Servlet API, as can be seen from the method declaration of Action's Execute
3) The code relies on Struts1 API, which can be seen from the Action class and FormBean class, Action must implement Action class of Struts

The reason why Struts2 takes THE design idea of WebWork as its core is that one point is the recent rise of WebWork, and the other is that WebWork does not have the disadvantages of Struts1, which is more in line with the design idea of MVC and more conducive to code reuse.

As can be seen from the above, Struts2 architecture is quite different from Struts1 architecture. Struts1 USES ActionServlet as its central processor, while Struts2 USES one interceptor (FilterDispatcher) as its central processor. One advantage of this is that it separates the Action class from Servlet API.

The simple process of Struts2 is as follows:

1) The browser sends the request
2) The central processor looks up the corresponding Action class to process the request according to the ES151en.xml file
3) The interceptor chain of WebWork automatically applies general functions to requests, such as WorkFlow, Validation and other functions
4) If the Method parameter is configured in the ES159en.xml file, then the Method method in the Action class corresponding to the Method parameter is called; otherwise, the generic Execute method is called to handle the user request
5) Send the response of the corresponding method in Action class to the browser

3. Comparison between Struts2 and Struts1

1) Implementation method of Action Class:
Action of Struts1 must extend the Action class or subclass of Action when it is implemented. The Action class of Struts2 can be implemented without any class or interface. Although an ActionSupport class is provided in Struts2, it is not required.
2) The Action class of Struts1 is singleton mode and must be designed to be thread-safe, while Struts2 produces one instance per request
3) The Action class of Struts1 depends on Servlet API. It can be seen from the method signature of execute that the execute method has two parameters of Servlet HttpServletRequest and HttpServletResponse, while Struts2 does not depend on Servlet API
4) Considering that Struts1 relies on Servlet API and other Web elements, it is very difficult to test Struts1's Action. With the help of other testing tools, Action of Struts2 can be tested like Service class 1 of other Model layers
5) Action and View of Struts1 transmit data through ActionForm or its subclasses. Although there are also ActionForm like LazyValidationForm, it is still impossible to transmit data through a simple POJO like other layers, but Struts2 turns such an aspiration into reality
6) Struts1 binds JSTL, which brings convenience to page writing. Struts2 integrates ONGL and can also use JSTL. Therefore, the expression language under Struts2 is more powerful

4. Comparison between Struts2 and WebWork

Struts2 is actually WebWork2.3, however, Struts2 is slightly different from WebWork:
1) Struts2 no longer supports the built-in IOC container, instead, Spring's IOC container is adopted
2) Struts2 For Webwork 1 some Ajax features, the label is replaced by Dojo


Related articles: