Current location - Quotes Website - Personality signature - The difference between struts and struts2
The difference between struts and struts2
In fact, Struts2 is not a strange Web framework. Struts2 is based on the design idea of Webwork, and absorbs the advantages of Struts 1. Therefore, Struts2 can be considered as the product of the combination of Struts 1 and Webwork.

Simply put, the difference between the two is:

One is Stuts 1 and the other is Stuts2, which is the biggest difference. Technically speaking, Stuts 1 has a core controller, but it only provides one interface, namely execute, and it is troublesome to configure actionform, so it has strong dependence. Stuts2 was developed for interceptors, which is the so-called AOP idea. You can configure multiple action, which is convenient to use, but the speed is a little slower than Stuts 1 because the interceptor before the request has some injection operations.

I. Introduction to MVC

Struts2 is an MVC framework compatible with Struts 1 and WebWork. Now that the MVC framework has been mentioned, we will give it a brief introduction, which is limited to a brief introduction. If you want to know more about MVC, you can consult relevant documents or find a book about Struts 1. I believe there will be a lot of room in MVC. Anyway, in fact, the ultimate goal of these frameworks appearing in Java at present is contact coupling. Whether it is Spring, Hibernate or MVC framework, the purpose is to increase reuse for contact coupling. MVC links 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 realization technology of each part can be summarized as follows:

1) model: JavaBean, EntityBean of EJB.

2) View: TagLib of JSP and Struts.

3) Controller: ActionServlet and Struts Action.

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

1) Multiple views can correspond to one model. According to MVC design pattern, one model corresponds to multiple views, which can reduce code duplication and code maintenance, and is easy to maintain once the model changes.

2) The data returned by the model is separated from the display logic. Any display technology can be applied to model data, 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 layers and provides the scalability of the application.

4) The concept of control layer is also effective, because it combines different models and different views to complete different requests. Therefore, the control layer can be said to contain the concept of user requesting permission.

5)MVC is more consistent with the spirit of software engineering management. Different layers have their own functions, and the components of each layer have the same characteristics, which is beneficial to generate management program code through engineering and instrumentalization.

The concept of MVC ends here, and the key point is what the implementation technology of each module is.

Secondly, briefly introduce Struts2.

Struts2 is developed from Struts 1, but in fact, Struts2 and Struts 1 are quite different in frame design ideas. Struts2' s design idea based on WebWork. Why doesn't Struts2 follow the design idea of Struts 1? After all, Struts 1 still has a very large market in the current enterprise application. So, let's look at Struts 65438.

1) supports single presentation layer technology.

2) It is seriously coupled with Servlet API, which can be seen from the method declaration of Execute of Action.

3) The code relies on Struts 1 API, which is intrusive, which can be seen from the time of writing Action class and FormBean. The operation must implement the operation class of Struts.

The reason why WebWork is the core of Struts2 is that WebWork is emerging recently. WebWork has no shortcomings of Struts 1, which is more in line with MVC design idea and more conducive to code reuse.

Based on the above introduction, we can see that the architecture of Struts2 is very different from that of Struts 1. Struts 1 uses ActionServlet as its central processor, while Struts2 uses FilterDispatcher as its central processor. One advantage of this is that the Action class and Servlet API are separate.

The simple processing flow of Struts2 is as follows:

1) browser sends the request.

2) The central processor searches the corresponding action class for processing the request according to the struts.xml file.

3)WebWork's interceptor chain automatically applies common functions to requests, such as workflow, verification and other functions.

4) If the method parameters are configured in the Struts.xml file, call the method in the Action class corresponding to the method parameters; Otherwise, the general Execute method is called to process the user request.

5) Respond the results returned by the corresponding methods in the Action class to the browser.

3. Comparison between Struts 2 and Struts 1

1) Action class implementation:

The Action of Struts 1 must be implemented by extending the Action class or its subclasses. The Action class of Struts2 can be implemented without any classes and interfaces. Although the ActionSupport class is provided in Struts2, it is not necessary.

2) The Action class of Struts1is singleton mode and must be designed to be thread-safe, and Struts2 generates an instance for each request.

3) The Action class of Struts1depends on Servlet API. As can be seen from the signature of its execute method, the execute method has two servlet parameters, HttpServletRequest and HttpServletResponse, and Struts2 does not depend on Servlet API.

4) It is considered that Struts 1 depends on Servlet API, so it is very difficult to test the action of Struts 1, and it needs to be tested by other testing tools. Struts2 actions can be tested like other service classes in the model layer.

5) Actions and views of Struts1transmit data through ActionForm or its subclasses. Although there are ActionForms such as LazyValidationForm, they still can't transmit data through a simple POJO like other levels, and Struts2 has turned this extravagant hope into reality.

6)Struts 1 binds JSTL, which brings convenience to page writing. Struts2 integrates ONGL and can also use JSTL, so the expression language under Struts2 is more powerful.

4. Comparison between Struts 2 and WebWork

Struts2 is actually WebWork2.3. However, Struts2 is slightly different from WebWork:

1)Struts2 no longer supports the built-in IOC container, and uses Spring's IOC container instead.

2)Struts2 replaces some Ajax features of Webwork with Dojo.