一、 新建自动转换工具配置文件
xwork-conversion.properties
内容:
java.util.Date=util.DateConvert
二、 util/DateConvert类
//继承系统StrutsTypeConverter类
public class DateConvert extends StrutsTypeConverter {
private final SimpleDateFormat[] sdfArr = {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyy年MM月dd日") };
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
System.out.println("进入转换器");
String dateStr = arg1[0];
Date dateResult = null;
for (int i = 0; i < sdfArr.length; i++) {
try {
dateResult = sdfArr[i].parse(dateStr);
System.out.println(dateResult);
break;
} catch (ParseException e) {
System.out.println("第"+(i+1)+"种转换失败:"+e.getMessage());
continue;
}
}
return dateResult;
}
@Override
public String convertToString(Map arg0, Object arg1) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String strResult=sdf.format(arg1);
System.out.println(strResult);
return null;
}