The depth comparison between Struts2 and struts1.x

Source: Internet
Author: User
Tags comparison continue 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.

First, the 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.

Two, the difference of action

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 is hello from strtuts2. Now is: " + DateFormat.getInstance().format( new Date());
     return SUCCESS;
  }
}

First, as you can see from the 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.

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.