ArticleDirectory
Receive Request Parameters Using Basic Types
(Get/post)
Define an attribute with the same name as the request parameter in the action class. Then, struts2 can automatically receive the request parameter and assign it to the attribute with the same name.
The Request Path is http: // localhost: 8080/test/view. Action? Id = 78
Public ClassProductaction {
PrivateInteger ID;
Public VoidSetid (integer ID ){//Struts2 uses the reflection technique to call the setter method of the attribute with the same name as the request parameter to obtain the request parameter value.
This. ID = ID;
}
PublicInteger GETID (){ReturnID ;}
}
Request Parameters received using compound types
The Request Path is http: // localhost: 8080/test/view. Action? Product. ID = 78
Public ClassProductaction {
PrivateProduct product;
Public VoidSetproduct (product ){This. Product = product ;}
PublicProduct getproduct (){ReturnProduct ;}
}
Struts2 first calls the default constructor of product to create the product object through reflection technology, and then calls the setter method of the property in the product with the same name as the request parameter to obtain the request parameter value. Custom type converter
Java. util. date can receive request parameter values in the format. However, if we need to receive Request Parameters in the format of 20091221, we must define the type converter; otherwise, struts2 cannot automatically complete the type conversion.
ImportJava. util. date;
Public ClassHelloworldaction {
PrivateDate createtime;
PublicDate getcreatetime (){
ReturnCreatetime;
}
Public VoidSetcreatetime (date createtime ){
This. Createtime = createtime;
}
}
Public Class Dateconverter Extends Defaulttypeconverter {
@ Override Public Object convertvalue (MAP context, object value, class totype ){
Simpledateformat dateformat = New Simpledateformat ("yyyymmdd ");
Try {
If (Totype = date. Class ){ // When the string is converted to the date type
String [] Params = (string []) value; // Request. getparametervalues ()
Return Dateformat. parse (Params [0]);
} Else If (Totype = string. Class ){ // When date is converted to a string
Date = (date) value;
Return Dateformat. Format (date );
}
} Catch (Parseexception e ){}
Return Null ;
}
}
Register the above type converterLocal Converter:
Place it under the package where the action class is locatedActionClassName-conversion.propertiesFile, actionclassname is the action class name, followed by-conversion. properties is a fixed writing method, for this example, the file name should beHelloWorldAction-conversion.properties. The content in the properties file is:
Attribute name = full Class Name of the type converter
For this example, the content in the HelloWorldAction-conversion.properties file is:
Createtime = com. Jim. Conversion. dateconverter
Custom global Converter
Register the above type converter as a global type converter:
InWEB-INF/classesPlacement belowXwork-conversion.propertiesFile. The content in the properties file is:
Type to be converted = full Class Name of the type converter
For this example, the content in the xwork-conversion.properties file is:
Java. util. Date = com. Jim. Conversion. dateconverter
You can use the struts2 built-in type converter strutstypeconverter
Public Class User2converter Extends Strutstypeconverter {
@ Override
Public Object convertfromstring (MAP context, string [] values, class toclass ){
String val = values [0];
Stringtokenizer ST = New Stringtokenizer (Val ,";");
String username = ST. nexttoken ();
String Password = ST. nexttoken ();
User user = New User ();
User. setusername (username );
User. setpassword (password );
Return User;
}
@ Override
Public String converttostring (MAP context, object O ){
User user = (User) O;
String username = user. GetUserName ();
String Password = user. GetPassword ();
Return "Username:" + username + ", password:" + password;
}
}