springmvc配置全局的日期转换器
来源:互联网 发布:重庆seo推广服务 编辑:程序博客网 时间:2024/05/20 06:07
为什么要配置日期转换器呢?
因为springmvc没有内置转换,当你提交日期格式的数据后传到后台后默认当作String类型的数据,就会报
HTTP Status 500 - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date]: no matching editors or conversion strategy found
1.在springmvc配置文件中配置
<!-- 配置全局的日期转化器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="cn.itcast.core.web.CustomDateEdtor"/>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="cn.itcast.core.web.CustomDateEdtor"/>
</property>
</bean>
2编写实现类实现WebBindingInitializer接口
package cn.itcast.core.web;
import java.text.DateFormat;
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.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
/**
* 日期转换器
* @author Bertram
*
*/
public class CustomDateEdtor implements WebBindingInitializer{
public void initBinder(WebDataBinder binder, WebRequest request) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
import java.text.DateFormat;
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.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
/**
* 日期转换器
* @author Bertram
*
*/
public class CustomDateEdtor implements WebBindingInitializer{
public void initBinder(WebDataBinder binder, WebRequest request) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
1 0
- springmvc配置全局的日期转换器
- SpringMVC配置全局日期转换器
- springmvc配置全局日期转换器
- SpringMVC配置全局日期转换器,处理日期转换异常
- SpringMVC配置全局日期转换器,处理日期转换异常
- SpringMVC配置全局日期转换器,处理日期转换异常
- springmvc配置全局日期转换器(ssm四)
- javaEE配置全局日期转换器
- springmvc配置自定义日期转换器
- springmvc全局日期的转换
- springmvc的日期转换器和编码过滤器
- springmvc日期格式转换器
- springMVC配置全局日期格式转化
- Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常
- springMVC配置消息转换器
- springmvc转换器配置
- SpringMVC+FastJson 自定义日期转换器
- SpringMVC+FastJson 自定义日期转换器
- 未来 已来:读《人工智能的未来》札记
- 证书Certificate以及android打包签名
- UML类图几种关系的总结
- 代码设计阶段基本思想
- jQuery选择器总结
- springmvc配置全局的日期转换器
- Android快速开发框架之xUtils---注解模块
- C++ 从文件中提取特定字符串
- opencv 鼠标操作 setMouseCallback
- C与C++学习笔记 --- g++ 和 gcc 学习笔记
- 建立https链接的SLL验证证书失效问题
- Retrofit中如何正确的使用https和证书锁定
- java 问题 无法解析类型 java.lang.CharSequence。从必需的 .class 文件间接引用了它
- Android之SDK、NDK、JNI和so文件