SpringMVC映射Date类型

来源:互联网 发布:python公约数 编辑:程序博客网 时间:2024/04/26 19:28

在Spring MVC中映射Date类型, 默认Spring会把Date类型注册为String类型, 在2.5版本之前需要自定义一个CustomEditor,自定义的类需要继extendsSimpleFormController,重写其initBinder方法;而在2.5之后的版本,就可以直接通过注解的方式处理了@InitBinder, 代码如下:

@InitBinderpublic void initBinder(ServletRequestDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");dateFormat.setLenient(true);binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}

这样注册之后, Date类型就可以正确使用了,不会出现类型转换错误了。

0 0