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
相关文章推荐
- Struts2之自定义局部类型转换器、全局类型转换器
- struts2示例代码-自定义局部类型转换器和自定义全局类型转换器
- Struts2之自定义局部类型转换器、全局类型转换器
- struts2中配置全局日期类型转换器
- Struts2自定义类型转换器分为局部类型转换器和全局类型转换器
- Struts2(二)局部与全局类型转换器
- Struts2实现自定义转换器和全局类型转换器
- struts2-局部类型和全局类型转换器
- Struts2之自定义局部类型转换器、全局类型转换器
- Struts2之自定义局部类型转换器、全局类型转换器
- 基于Struts2的全局类型转换器
- Struts2 全局类型转换器代码 时期
- struts2学习(二)--转换器StrutsTypeConverter和全局类型转换
- Struts2实现自定义转换器和全局类型转换器
- struts2 自定义全局类型转换器
- Struts2的自定义类型转换器实例
- [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器
- struts2类型转换器入门
- struts2自定义类型转换器
- struts2自定义类型转换器