频道栏目
首页 > 资讯 > 其他 > 正文

Struts2_全局类型转换器

17-05-20        来源:[db:作者]  
收藏   我要投稿
	
自定义全局类型转换器:
	public Object convertValue(Map 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

相关TAG标签
上一篇:ssm和ssh框架中,oracle数据库,表主键自增如何解决
下一篇:Android studio 设置五颜六色的logcat
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站