标签:
SpringMVC类型转换问题: String转换Date问题.
方法很多,就不一一罗列了.我就列举三种方法:
第一种是采用SpringMVC里面的@initBinder注解.(可以写一个全局的)
@InitBinder
protected void initBinder(WebDataBinder binder) {
//指定需要转换的日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//注册日期编辑器
//Spring还提供了很多的类型编辑器
//如CustomBooleanEditor、CustomNumberEditor、FloatEditor、DoubleEditor、LongEditor、CustomNumberEditor、IntegerEditor
//基本能满足开发
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
第二种是在对应实体类属性中加上@DateTimeFormat(pattern="yyyy-MM-dd"),并且在项目中导入joda-time-2.8.1.jar。
第三种就是在Controller中多声明一个参数,用String类型接收,然后通过一些日期工具类, 或者Calendar,SimpleDateFormat实现数据类型转换.
标签:
原文地址:http://www.cnblogs.com/xash/p/5012264.html