The depth comparison between struts1.x and Struts2

Source: Internet
Author: User
Tags aop dateformat

As a web framework of MVC 2, struts has been pursued by developers since its inception and has been widely used. As the most successful web framework, struts naturally has many advantages: the use of the MVC 2 model, the full-featured tag library (tag libraries), and the open source code. However, the so-called "no best, only better", struts1.x itself has a lot of shortcomings: need to write too much code, easy to cause "class explosion", unit testing difficulties. These disadvantages are becoming more and more obvious with the development of the Web. This gives rise to struts 2, its birth can be a good solution to the above problems. In this paper, the author will make a detailed comparison between the two frameworks of Struts2 and struts1.x. Comparisons will relate to action, validation, type conversion, and how to develop these two frameworks. Hope that through this comparison, let the reader understand the characteristics of the two frameworks, so that in their own projects, according to the actual situation, as soon as possible to transition to the Struts2 era. The content of this article is based on Struts2.0.6.One,Introduction  The first version of Struts was released in May 2001. It was originally conceived to be a clear separation of the view and business/application logic of Web applications by combining JSP and servlet. Before struts, the most common practice is to include business and application logic in the JSP, or to generate views in a servlet through println (). Since the release of the first edition, Struts has actually become an industry-recognized Web application standard. Its popularity has also brought improvements and changes to its own, so it will not only keep up with the changing needs of the Web application framework, but also with the characteristics of a growing number of competing frameworks. In the end, several next-generation struts solutions are produced. Among the two most watched programs are shale and struts Ti. Shale is a component-based framework and has recently become a top-level project for Apache. And Struts Ti is based on the successful experience of struts continue to adhere to the front-end controller (Front Controller) and MVC (Model-view-controller) mode to improve. The WebWork project, released in March 2002, revolutionized the struts framework by introducing new ideas, concepts, and features, but not compatible with the original struts code. WebWork is a mature framework that has undergone several major improvements and releases. In December 2005, WebWork and Struts TI announced the merger. At the same time, struts TI was renamed the Struts Action Framework 2.0 to become the true successor of struts. Finally, it's not that struts or webwork projects have stopped developing. Since interest in these two projects is still high, and many developers are still willing to use them, the two projects continue to be developed, continue to fix bugs, improve functionality, and continue to add new features.Second,Actionthe Difference  For friends with rich struts1.x development experience, it is clear that action is the core of the entire struts framework, and of course Struts2 is no exception. However, struts1.x differs greatly from the STRUTS2 action model. The difference between Struts2 and struts1.x, the most obvious is that STRUTS2 is a PULL-MVC structure. What do you mean by that? From a developer's point of view, the data that needs to be displayed to the user can be obtained directly from the action, unlike struts1.x, which has to be stored in page, request, or session to get the appropriate bean. struts1.x must inherit org.apache.struts.action.Action or its subclasses, and the form data is encapsulated in Formbean. Struts 2 does not need to inherit any type or implement any interface, the form data is contained in the action, obtained by Getter and setter (as in the following ACTIONFORSTRUTS2 code example). Although theoretically Struts2 action does not need to implement any interfaces or inherit any of the classes, in the actual programming process, in order to more easily implement the action, In most cases, the Com.opensymphony.xwork2.ActionSupport class is inherited and the string execute () method in this class is overloaded (Override). As shown below:

Package actiondiffer; Import Java.text.DateFormat; Import Java.util.Date; Import Com.opensymphony.xwork2.ActionSupport; public class ActionForStruts2 extends Actionsupport ... {private string message, public string getMessage () ... {return message;} @Override public String execute () ... {message = ' This are hello from strtuts2. Now is: "+ dateformat.getinstance (). Format (new Date ()); return SUCCESS; First, as you can see from ActionForStruts2, the object returned is not Actionforward, but string. If you don't like to appear in your code as a string, there's a helper interface action that provides a constant way to provide common results, such as "success", "None", "Error," "Input," and "Login." In addition, by convention, only the "execute" method can invoke the action in struts1.x, but it is not necessary in Struts2, and any declared as a public String methodname () method can be configured to invoke the action. Finally, the most revolutionary difference with struts1.x is that the method invoked during the STRUTS2 process (the "execute" method) is not parametric. So how do you get the objects you need? The answer is to use the IOC (reverse control, inversion of controls), also known as the "Dependency injection (Dependency injection)" model (for more information, see Martin Fowler's article http:// www.martinfowler.com/articles/injection.html). The spring framework has made this pattern popular, but Struts2 's predecessor (WebWork) has also applied this pattern.
Third, the IoCIoC (Inversion of Control, translated below), is increasingly familiar with the spread of lightweight containers (lightweight Contianer) in the Java community. It is no longer necessary to explain "what is control reversal" and "why control reversal". Because there are so many articles on the Internet that have a wonderful and accurate answer to such questions. Readers can read "Development of" by Rod Johnson and Juergen Hoeller, Expert one-on-one Java EE without Fowler EJB or Martin inversion. Control containers and the "Dependency injection pattern".
As we all know, STRUTS2 is developed on the basis of WebWork 2. The webwork version of WebWork 2.2, which has its own set of control reversal implementations, WebWork 2.2, in the context of the spring framework's booming development, decided to abandon the development of the control reversal function and turn it into a spring implementation. It is worth mentioning that spring is indeed a learning framework, as more and more open source components, such as Ibatis, are abandoning the development of overlapping features with spring. Therefore, STRUTS2 recommends that you implement control inversion through spring.
To better understand inversion control, let's look at an example of how IOC can access the current request Httpserverrequest object during the action process.
In the example, the dependency injection mechanism used is interface injection. Like its name, interface injection requires an interface that has already been implemented. This interface contains the setter of the corresponding property, providing a value for the action. The Servletrequestaware interface is used in the example, as follows: public interface Servletrequestaware {
public void Setservletrequest (HttpServletRequest request);
When this interface is inherited, the simple action may seem a bit complicated, but then you can get the Httpserverrequest object to use. public class IoCForStruts2 implements Servletrequestaware {
private HttpServletRequest request;
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
Public String Execute () throws Exception {
You can start working with the Request object
return action.success;
}
It appears that these properties are class-level and are not thread-safe and can cause problems. In fact, there is no problem with Struts2, because each request comes with a new instance of the action object, it does not share an object with other requests, so there is no need to consider thread safety issues. Four, Intercepting DeviceInterceptor (The following is an interceptor), used in AOP (aspect-oriented programming) to intercept a method or field before it is accessed, and then add some action before or after. Interception is an implementation strategy for AOP.
The Chinese document in WebWork is interpreted as-the interceptor is the object that dynamically intercepts the action call. It provides a mechanism for developers to define code that executes before and after an action is executed, or to block execution of an action before it is executed. It also provides a way to extract the reusable parts of the action.
Struts1.x's standard framework does not provide any form of interceptor, although an add-on project called Saif implements such a function, but its scope is limited.
Interceptors are a powerful tool for Struts2, and many functions (feature) are built on top of it, such as internationalization, converters, checksums, and so on. When it comes to interceptors, there is also a popular word-the Interceptor chain (Interceptor Chain, called the Interceptor Stack interceptor stack in Struts2). The Interceptor chain is a chain that connects the interceptor in a certain order. When you access an intercepted method or field, the interceptor in the interceptor chain is invoked in the order in which it was previously defined.
The interceptors for Struts 2 are relatively simple to implement. When the request arrives at Struts2 's Servletdispatcher, Struts 2 looks for the configuration file and instantiates the relative interceptor object based on its configuration, then strings it into a list (list), and finally invokes the interceptor in the list, as shown in Figure 1.



Struts 2 already offers a wide variety of full-featured interceptor implementations. Readers can view the configuration of the default interceptor and interceptor chains to the struts-default.xml of the Struts2-all-2.0.6.jar or Struts2-core-2.0.6.jar package. As a "framework", scalability is indispensable because there is no universal thing in the world. Although Struts 2 provides us with such a rich interceptor implementation, it does not mean that we lose the ability to create custom interceptors, but on the contrary, it is quite easy to customize interceptors in Struts 2. Five, Struts2 and struts1.x The overall comparison

In order to make a comprehensive comparison between Struts2 and strtus1.x, the reader is informed of the pros and cons of the two frameworks, so that in their own projects, according to the actual situation, select the appropriate framework, compare them to each other, summed up the following table analysis and comparison.

characteristic

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.