Struts2_全局类型转换器

来源:互联网 发布:windows开发 编辑:程序博客网 时间:2024/06/05 14:42
自定义全局类型转换器: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