springMVC注解@initbinder日期类型的属性自动转换
2016-11-23 15:33
393 查看
在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能。
一、控制器中代码
比较简单的可以直接应用springMVC的注解@initbinder和spring自带的WebDataBinder类和操作,controller中配置了initBinder()时则再接收String型的日期时会自动转换。
二、springMVC中配置
此篇内容参考:http://blog.csdn.net/jiubugeinifo/article/details/41678717
一、控制器中代码
比较简单的可以直接应用springMVC的注解@initbinder和spring自带的WebDataBinder类和操作,controller中配置了initBinder()时则再接收String型的日期时会自动转换。
package com.shiliu.game.controller; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; public class InitController { /** * 自动转换日期类型的字段格式 */ @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, 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"> <property name="supportedMediaTypes"> <list> <value>text/html; charset=utf-8</value> </list> </property> </bean>
此篇内容参考:http://blog.csdn.net/jiubugeinifo/article/details/41678717
相关文章推荐
- springMVC注解@initbinder,表单中的日期 字符串和Javabean中的日期类型的属性自动转换
- springMVC注解@initbinder日期类型的属性自动转换
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- 详解SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- SpringMVC注解@initbinder解决类型转换问题
- initBinder解决Json日期转换为date类型
- SpringMVC之自定义类型转换WebBindingInitializer@initBinder
- spring类型自动转换——@InitBinder和Converter
- SpringMVC之类型转换@initBinder
- springMVC类型转换,提交日期字符数据自动转换日期格式
- springMVC类型转换,@initBinder使用
- springMVC中返回json时,日期类型自动转换为long类型的时间戳的处理方案(一个配置解决)
- 关于springmvc怎么自动把前台string类型日期字段转换成date类型
- SpringMVC日期类型自动转换的处理
- SpringMVC之类型转换@initBinder
- Springmvc中对日期类型的属性转换问题和自定义的参数解析器