概述:
在看代码的时候,发现有段代码用了这个注解@InitBinder。
百度一下,原来是为了解决页面上提交的时间控件转换的问题。字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能。
比较简单的可以直接应用springMVC的注解@initbinder和spring自带的WebDataBinder类和操作。
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}