您的位置:首页 > 编程语言 > Java开发

Struts2_全局类型转换器

2017-05-17 16:51 411 查看
自定义全局类型转换器:
public Object convertValue(Map<String, Object> context, Object value, Class toType) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
try {
if (toType == Date.class) {// 要转换的参数类型value(birthday),toType(Date)
// 把参数转成字符串数组,再把字符串数据转换成Date类型
String[] params = (String[]) value;
return dateFormat.parse(params[0]);
} else if (toType == String.class) {// 要转换的参数类型value(birthday),toType(Date)
// 把参数转成Date类型,再把Date类型转成字符串
Date date = (Date) value;
System.out.println(date+"String.class:" + toType.getClass());
return dateFormat.format(date);
}

} catch (Exception e) {
}
return null;
}
将上面的类型转换器注册为全局类型转换器:
在WEB-INF/classes下放置xwork-conversion.properties文件。在properties
文件中的内容为:
待转换的类型=类型转换器的全类名
对于本例而言,xwork-conversion.properties文件中的内容为:
java.util.Date=cn.itcast.e_action.DateTypeConverter
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: