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

在实际操作中经常会碰到表单中的日期 字符串和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:不能为空值
     }

 

posted @ 2016-02-19 20:35  DMC_HZP  阅读(1368)  评论(0编辑  收藏  举报