SpringMVC注解@initbinder解决类型转换问题
来源:互联网 发布:外国知乎瞧不起中国 编辑:程序博客网 时间:2024/05/21 09:53
在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题。
在需要日期转换的Controller中使用SpringMVC的注解@initbinder
和Spring自带的WebDateBinder
类来操作。
WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到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类。
我们也可以不使用他们自带的这些编辑器类。
我们可以自己构造:
import org.springframework.beans.propertyeditors.PropertiesEditor;public class DoubleEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if (text == null || text.equals("")) { text = "0"; } setValue(Double.parseDouble(text)); } @Override public String getAsText() { return getValue().toString(); }}
0 0
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- Springmvc注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- springMVC注解@initbinder日期类型的属性自动转换
- springMVC类型转换,@initBinder使用
- SpringMVC之类型转换@initBinder
- SpringMVC之类型转换@initBinder
- SpringMVC之自定义类型转换WebBindingInitializer@initBinder
- springMVC注解@initbinder,表单中的日期 字符串和Javabean中的日期类型的属性自动转换
- springMVC注解@initbinder
- SpringMVC-08 @InitBinder注解
- springMVC注解@initbinder
- springMVC注解@initbinder
- springmvc @InitBinder()注解
- C++面试题
- HttpMessageConverter
- poj 2516
- 在{互联网}的大海中如何避免【SEO黑帽】
- gdb查找段错误
- SpringMVC注解@initbinder解决类型转换问题
- 【NOIP2015】信息传递
- Leetcode 202. Happy Number
- 初次使用python之在notepad++中直接运行python代码
- 浅拷贝与深拷贝的区别
- Windows下scala环境搭建
- spring mvc使用@InitBinder 标签对表单数据绑定
- MySQL从被虐到成长
- SpringMVC之类型转换@initBinder