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