Struts2 custom Type converters are divided into local Type converters and Global Type converters
(1) local type converter
If reg. action? Birthday = to the background action, and then the property can be received using the date type, but if the input is a string of the 20101112 type, it cannot be obtained using the date type, in addition, errors may occur. struts2 provides a type converter for us to use.
The following describes how to develop a local converter:
A. First, you must write a class to inherit defatypetypeconverter.
B. overwrite the convertValue method to perform data transformation.
C. Place the ActionClassName-conversion.properties file under the package where the action class is located, the ActionClassName is the class name, and the-conversion. properties is a fixed method,
Such as: HelloWorldAction-conversion.properties
D. The content in the Properties file is: property name = full Class Name of the type converter (both package name and class name)
For example, birthday = com. ljq. type. converter. DateTypeConverter
(2) Global Converter
If the business requires that all dates be converted, you can use the global type converter, as long as you place the xwork-conversion.properties file under the src root directory, and the content in the properties file is:
Type to be converted = full Class Name of the type converter
For example, java. util. Date = com. type. Converter. DateTypeConverter.
Code
Action class
Package com. ljq. action;
Import java. util. Date;
Public class HelloWorldAction {
// Date
Private Date birthday;
// Enumeration
Private Gender gender;
Public String execute (){
Return "success ";
}
Public Date getBirthday (){
Return birthday;
}
Public void setBirthday (Date birthday ){
System. out. println ("birthday =" + birthday );
This. birthday = birthday;
}
// Custom Enumeration
Public enum Gender {
MAN, WOMEN
}
Public Gender getGender (){
Return gender;
}
Public void setGender (Gender gender ){
System. out. println ("gender =" + gender );
This. gender = gender;
}
}
Date type converter
Package com. ljq. type. converter;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. Map;
Import com. opensymphony. xwork2.conversion. impl. DefaultTypeConverter;
/**
* Date custom type converter
*
* @ Author jiqinlin
*
*/
Public class DateTypeConverter extends DefaultTypeConverter {
@ SuppressWarnings ("unchecked ")
@ Override
Public Object convertValue (Map <String, Object> context, Object value,
Class toType ){
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd HH: mm: ss ");
Try {
If (toType = Date. class) {// when the string is converted to the Date type
String [] params = (String []) value;
Return sdf. parseObject (params [0]);
} Else if (toType = String. class) {// when Date is converted to a String
Date date = (Date) value;
Return sdf. format (date );
}
} Catch (java. text. ParseException e ){
E. printStackTrace ();
}
Return null;
}
}
Enumeration type converter
Package com. ljq. type. converter;
Import java. util. Map;
Import com. ljq. action. HelloWorldAction. Gender;
Import com. opensymphony. xwork2.conversion. impl. DefaultTypeConverter;
/**
* Enumeration custom type converter
*
* @ Author jiqinlin
*
*/
Public class GenderTypeConverter extends DefaultTypeConverter {
@ Override
Public Object convertValue (Map <String, Object> context, Object value,
Class toType ){
If (toType = Gender. class) {// when the string is converted to the Gender type
String [] params = (String []) value;
Return Gender. valueOf (params [0]);
} Else if (toType = String. class) {// when Gender is converted to a String
Gender gender = (Gender) value;
Return gender. toString ();
}
Return null;
}
}
Configuration type converter
Test path
Date
Http: // localhost: 8083/struts2/control/employee/list_execute.do? Birthday = 20110315 23:34:55
Enumeration
Http: // localhost: 8083/struts2/control/employee/list_execute.do? Gender = WOMEN
Local converter: HelloWorldAction-conversion.properties
Birthday = com. ljq. type. converter. DateTypeConverter
Gender = com. ljq. type. converter. GenderTypeConverter
Global converter: xwork-conversion.properties
Java. util. Date = com. ljq. type. converter. DateTypeConverter
Print the date and enumerated value on the page
Birthday =$ {birthday}
Gender =$ {gender}