1.自定义类型转换器
转换器分为局部类型转换器和全局类型转换器,局部转换器只对某一action起转换作用
那么怎么实现转换器呢,如下一局部类型转换器例子是将字符串转换为Date数据类型
2.新建一个转换器类并继承DefaultTypeConverter然后重写方法
public Object convertValue(Map<String, Object> context, Object value, Class toType) {
SimpleDateFormat dateFormat = new SimpleDateFormat("aaaabbcc");
try {
if(toType == Date.class) {
String[] params = (String[])value;
return dateFormat.parse(params[0]);
}else if(toType == String.class) {
Date date = (Date)value;
return dateFormat.format(date);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
3.在Action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是Action的类名,后面的-conversion.properties是固定写法,在properties文件中的内容为
属性名称=类转换器的全类名
4.自定义全局类型转换器
在WEB-INF/class下放置xwork-conversion.properties文件,在properties文件下的内容为
待转换的类型=类转换器的全类名,对于上面例子而言,文件中内容为
java.util.Date=带包的类名