Struts2 3 -- Packaging of Action classes

Source: Internet
Author: User
Tags object serialization

1: Let the preceding Action directly implement the Struts2 Framework Action interface. It provides several constants and an abstract execute method. The following is the Action interface provided by struts2.
// Define five string constants and return values in a uniform manner. The main function is to standardize the return value.
Public abstract interface com. opensymphony. xwork2.Action {
 
// Field descriptor #4 Ljava/lang/String;
Public static final java. lang. String SUCCESS = "success ";
 
// Field descriptor #4 Ljava/lang/String;
Public static final java. lang. String NONE = "none ";
 
// Field descriptor #4 Ljava/lang/String;
Public static final java. lang. String ERROR = "error ";
 
// Field descriptor #4 Ljava/lang/String;
Public static final java. lang. String INPUT = "input ";
 
// Field descriptor #4 Ljava/lang/String;
Public static final java. lang. String LOGIN = "login ";
 
// Method descriptor #16 () Ljava/lang/String;
Public abstract java. lang. String execute () throws java. lang. Exception;
}


2: Let the above Action inherit the ActionSupport (com. opensymphony. xwork2.ActionSupport) of the Struts2 framework. This class provides many special features of Struts2.
If the Action class inherits this ActionSupport class, the Action class can provide data verification, internationalization, Object serialization, and other functions. In addition, this ActionSupport implements the Action interface.

The following is the interface declaration to be implemented by this ActionSupport class.

Public class com. opensymphony. xwork2.ActionSupport
Implements com. opensymphony. xwork2.Action,
Com. opensymphony. xwork2.Validateable,
Com. opensymphony. xwork2.ValidationAware,
Com. opensymphony. xwork2.TextProvider,
Com. opensymphony. xwork2.LocaleProvider,
Java. io. Serializable {

... Related Solutions
}

 

Notes

The Actionsupport tool class defines a validate () method based on the implementation of the Action interface. to override this method, it will be executed before the execute () method, such as verification failure, will be transferred to the input field. The input attribute must be configured when this Action is configured.
If an error is detected, you can call this. addFieldError to add an error message to the framework. Then it is displayed on the page.
Example:

@ Override
Public void validate (){
If (null = this. getPasswd () | ". equals (this. getPasswd (). trim ())){
This. addFieldError (passwd, "passwd is required ");
}
If (null = this. getUsername () | "". equals (this. getUsername (). trim ())){
This. addFieldError (username, "username is required ");
}
}

 

In addition, Actionsupport also provides a getText (String key) method for internationalization. This method gets internationalized information from the resource file, which is very useful.

 


 


Author: weiguolee

Related Article

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.