springmvc中initbinder的作用
2016-11-08 11:50
204 查看
原文地址:http://blog.csdn.net/axin66ok/article/details/17938095
在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。解决的办法就是使用spring mvc提供的@InitBinder标签
在项目中一般都是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
@InitBinder
protected void initBinder(WebDataBinder binder) {
<span style="white-space:pre"> </span>binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
<span style="white-space:pre"> </span>binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));
binder.registerCustomEditor(int.class, new IntegerEditor());
<span style="white-space:pre"> </span>binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
binder.registerCustomEditor(long.class, new LongEditor());
binder.registerCustomEditor(double.class, new DoubleEditor());
binder.registerCustomEditor(float.class, new FloatEditor());
}
在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。解决的办法就是使用spring mvc提供的@InitBinder标签
在项目中一般都是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
@InitBinder
protected void initBinder(WebDataBinder binder) {
<span style="white-space:pre"> </span>binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
<span style="white-space:pre"> </span>binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));
binder.registerCustomEditor(int.class, new IntegerEditor());
<span style="white-space:pre"> </span>binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
binder.registerCustomEditor(long.class, new LongEditor());
binder.registerCustomEditor(double.class, new DoubleEditor());
binder.registerCustomEditor(float.class, new FloatEditor());
}
相关文章推荐
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC中利用@InitBinder来对页面数据进行解析绑定的方法
- springMVC-InitBinder
- SpringMvc @InitBinder 表单多对象精准绑定接收 ..
- SpringMVC 处理Date类型@DateTimeFormat <fmt:formatDate/> @InitBinder
- springMVC注解@initbinder,表单中的日期 字符串和Javabean中的日期类型的属性自动转换
- springMVC注解@initbinder日期类型的属性自动转换
- @InitBinder注解 解决form表单向springmvc传递多个对象参数
- SpringMVC之类型转换@initBinder
- 详解SpringMVC注解@initbinder解决类型转换问题
- SpringMVC处理表单日期数据转换异常(Date)使用@InitBinder
- SpringMVC中利用@InitBinder来对页面数据进行解析绑定
- springmvc 框架中的数据的绑定:@InitBinder&@DateTimeFormat()
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC之类型转换@initBinder
- springMVC注解@initbinder
- SpringMVC的@InitBinder注解使用
- SpringMvc @InitBinder 表单多对象精准绑定接收
- SpringMVC之自定义类型转换WebBindingInitializer@initBinder