有的业务可能会用到自定义类型转换器 比如表单提交中提交的日期 默认是String类型 需要封装到 日期对象中 这时 就需要自定义转换器
方法
首先在controller中启用@InitBinder 注解 来初始化 数据绑定器
然后写自己的方法(以日期转换为例) 当提交时 如果不是默认的普通类型的转换 就会调用下面的类型转换器就行格式转换
@InitBinder
protected void dataConverter(HttpServletRequest req, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}