Struts2 custom type converter
1.1. struts2 custom type converter
1) custom type conversion class, inheriting the defatypetypeconverter class
|
Package com. morris. ticket. conversion;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. Map;
Import com. opensymphony. xwork2.conversion. impl. DefaultTypeConverter;
Public class DateConversion extends DefaultTypeConverter {
Public Object convertValue (Map Context, Object value,
Class toType ){
String [] parameters = (String []) value;
// There are multiple parameters obtained from the name on the page. The first parameter is used.
String dateStr = parameters [0];
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");
// If the type to be converted is time
If (toType = Date. class ){
Try {
Return sdf. parse (dateStr );
} Catch (ParseException e ){
E. printStackTrace ();
}
}
Return null;
}
} |
2) configure the custom type converter and create a new xwork-conversion.properties file under the src directory
|
Java. util. Date: com. morris. ticket. conversion. DateConversion |