首页 > 编程语言 > 详细

springMVC使用@InitBinder注解把字符串转化为Date类型

时间:2016-02-19 21:49:58      阅读:1715      评论:0      收藏:0      [点我收藏+]

在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能。

比较简单的可以直接应用springMVC的注解@initbinder和spring自带的WebDataBinder类和操作

   @InitBinder
     public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值,false:不能为空值
     }

 

springMVC使用@InitBinder注解把字符串转化为Date类型

原文:http://www.cnblogs.com/dmchzp/p/5202120.html

(0)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!