Springmvc注解@initbinder解决类型转换问题
2017-10-08 16:48
281 查看
在使用SpringMVC的时候,经常会遇到表单中的日期字符和JavaBean
的 Date类型的转换,而SpringMVC默认不支持这个转换,所以需要手动设置,自定义数据的绑定才能解决这个问题。
在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。
WebDateBinder
是用来绑定请求参数到指定的属性编辑器.由于前台传到controller
里的值是String类型的,当往Model中Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的editor进行转换,然后在SET进去。
代码如下:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
需要在SpringMVC的配置文件加上
<!-- 解析器注册 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter"/>
</list>
</property>
</bean>
<!-- String类型解析器,允许直接返回String类型的消息 -->
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
换种写法
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
拓展:
spring mvc在绑定表单之前,都会先注册这些编辑器,Spring自己提供了大量的实现类,诸如CustomDateEditor
,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
使用时候调用WebDataBinder的registerCustomEditor方法
registerCustomEditor源码:
public void registerCustomEditor(Class<?>requiredType, PropertyEditor propertyEditor) {
getPropertyEditorRegistry().registerCustomEditor(requiredType,propertyEditor);
}
第一个参数requiredType是需要转化的类型。
第二个参数PropertyEditor是属性编辑器,它是个接口,以上提到的如CustomDateEditor等都是继承了实现了这个接口的PropertyEditorSupport类。
我们也可以不使用他们自带的这些编辑器类。
的 Date类型的转换,而SpringMVC默认不支持这个转换,所以需要手动设置,自定义数据的绑定才能解决这个问题。
在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。
WebDateBinder
是用来绑定请求参数到指定的属性编辑器.由于前台传到controller
里的值是String类型的,当往Model中Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的editor进行转换,然后在SET进去。
代码如下:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
需要在SpringMVC的配置文件加上
<!-- 解析器注册 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter"/>
</list>
</property>
</bean>
<!-- String类型解析器,允许直接返回String类型的消息 -->
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
换种写法
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
拓展:
spring mvc在绑定表单之前,都会先注册这些编辑器,Spring自己提供了大量的实现类,诸如CustomDateEditor
,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
使用时候调用WebDataBinder的registerCustomEditor方法
registerCustomEditor源码:
public void registerCustomEditor(Class<?>requiredType, PropertyEditor propertyEditor) {
getPropertyEditorRegistry().registerCustomEditor(requiredType,propertyEditor);
}
第一个参数requiredType是需要转化的类型。
第二个参数PropertyEditor是属性编辑器,它是个接口,以上提到的如CustomDateEditor等都是继承了实现了这个接口的PropertyEditorSupport类。
我们也可以不使用他们自带的这些编辑器类。
相关文章推荐
- SQLServer类型与Java类型转换问题解决
- 解决Struts2中转换Date类型的问题
- “error C2664: “MessageBoxW”: 不能将参数 2 从“char [300]”转换为“LPCWSTR” 1> 与指向的类型无关;转换要求 reinterpret_”问题解决
- 怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转换。从: 1252 到: 936
- 解决问题:SSIS中,列“column”无法在 unicode和非unicode字符串数据类型之间转换
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)
- MYSQL复制源文件 丢失问题解决, innoDB 类型转换为 MyISAM
- SpringMVC注解@initbinder解决类型转换问题
- Swift:(十一)、构造器、自动引用计数、循环引用问题解决、类型转换
- 在VS2005/2008中字符串编码问题的解决及字符串和数值类型常用的转换方法
- MYSQL复制源文件 丢失问题解决, innoDB 类型转换为 MyISAM
- ASP.NET遇到问题,“对象不能从 DBNull 转换为其他类型”,记录一下~问题已经解决,谢谢大家~
- SpringBank 开发日志 重新设计Action调用Service的参数传递 使用泛型解决类型转换问题
- JSON(3):Java的Date类型转换为符合json语法的字符串遇到的问题及其解决办法
- 解决spring使用动态代理类型转换失败的问题--java.lang.ClassCastException: com.sun.proxy.$Proxy$ cannot be cast to ...今天
- mysql数据库中“0000-00-00 00:00:00”格式无法转换成datatime类型的问题解决方法
- 解决struts2基础数据类型自动转换,输入字母报错问题!
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- 解决Postgresql 的强类型转换运算符::与hibernate冲突的问题