Difference between Struts2 and Struts: struts2struts

Source: Internet
Author: User

Difference between Struts2 and Struts: struts2struts

Difference between Struts2 and Struts

From the development process of Struts2, Struts2 inherits the blood of Struts and WebWork. Struts2 uses the essence of the two to form a new German framework. However, struts2 still inherits the design philosophy of struts, and added the advantages of WebWork. Before learning about the Struts2 system, let's take a look at the modifications made by struts2 to struts.

The differences between Struts2 and struts are mainly reflected in the following aspects.

(1) Action class

 

1. In struts, abstract classes are used for programming rather than interfaces. Therefore, the Development Action class is required to inherit an abstract base class, such as DispatchAction.

 

2. Struts2 is based on Interface Programming. Our Action class can implement an Action interface to make optional and customized services possible. Struts2 provides an ActionSupport base class to implement common interfaces. What's more powerful is that the Action interface is not necessary in struts2. Any pojo object with the execute () function can be used as the Action class of struts2.

 

(2) thread mode

 

1. struts Action is a singleton mode and must be thread-safe, because only one instance of Action is used to process all requests. The single-column policy limits what struts actions can do and requires special attention during development. Action resources must be thread-safe or synchronized;

 

2. The Action object of struts2 generates an instance for each request, so there is no thread security problem.

 

(3) Servlet dependency

 

1. struts actions depend on Servlet APIs. When an Action is called, HttpServletRequest and HttpServletResponse are passed to the execution method. For example:

 

PublicActionForward execute (Actionmapping mapping, ActionForm Form, HttpServletRequestrequest, HttpServletResponse response ){

 

}

 

2. The Action of struts2 does not depend on the container and allows the Action to be tested independently from the container. If necessary, the Struts2 Action can access the initial request and response. For example, the following class MyAction. Java can get the Session value through the ActionContext object or the request object through the ServletActionContext object.

 

Publicclass MyAction {

Protected Object getSession (String key ){

Return ActionContext. getContext (). getSession (key );

}

ProtectedHttpServletRequest getRequest (){

Return (String) ServletActionContext. getRequest ();

}

}

 

(4) Testability

 

1. A major problem in testing Struts actions is that the execute () method exposes the Servlet API, which makes the test dependent on the container. Although there is a third-party software struts TestCase that can use simulated objects for testing, it will obviously make our testing more complex.

 

2. Struts2 Action class is a POJO object. Therefore, you can directly write a test class to test the Action class. You only need to inject attributes into the test class.

 

(5) Capture Input

 

1. struts uses the ActionForm object to capture user input. All actionforms must inherit one base class ActionForm. Because other JavaBean cannot be used as an ActionForm, developers often create redundant class capture inputs. Dynamic forms can be used as an option to create traditional actionforms. However, developers may re-create existing JavaBean, which still leads to redundant JavaBean;

 

2. struts2 uses the Action attribute as the INPUT attribute to eliminate the need for the second input object. The INPUT attribute may be a rich object type with its own sub-attributes. The Action attribute can be accessed through the taglibs on the web page. At the same time, struts2 also supports the ActionForm mode.

 

(6) Expression Language

 

1. struts integrates JSTL. This EL has basic object graph traversal, but its support for set and index attributes is weak.

 

2 struts2 can use JSTL, but it also supports a stronger and more flexible Expression Language OGNL

 

(7) bind the value to the page

 

1. struts uses the standard jsp mechanism to bind objects to pages for access

 

2. struts2 uses the ValueStack (value stack) technology to enable taglib to access values without binding your page (view) to an object. The ValueStack policy allows you to reuse a page (view) using a series of attributes with the same name but different types)

 

(8) type conversion

 

1. The ActionForm attribute of struts is usually of the String type and does not use Commons-Beanutils for type conversion. Each class provides a converter, which is not configurable for each instance;

2. struts2 uses OGNL for OGNL type conversion and provides basic and common object converters.

 

(9) data verification

 

1. struts supports manual verification in the Validate method of ActionForm, or extended verification through Commons Validator. The same class may have different verification content, but it cannot verify the child object;

2. struts2 supports verification by using the validata () method and the XWork verification framework. The XWork verification framework uses the validation content defined for the attribute type to support the chain validation subattribute.

(10) Control of Action execution

 

1. struts supports a separate Request Processors (lifecycle) for each module, but all actions in the module must provide the same lifecycle;

 

2. struts2 supports creating different lifecycles for each Action through Interceptor Stacks. The stack can be used with different actions as needed.

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.