spring mvc开发接收日期字段表单提交,自动转换成Date类型报错,解决办法
来源:互联网 发布:淘宝夜间模式 编辑:程序博客网 时间:2024/06/15 21:23
User中有birthday(Date)属性,用户注册的时候,选择日期即可,然后提交表单,可spring mvc 报错,意思是不能把字符串转为Date类型的。如果是strtus的话,压根不是问题,怎么到spring mvc就不行了呢,可能有好的解决办法
方法一:实体类中加日期格式化注解
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
如上,在对应的属性上,加上指定日期格式的注解,本人亲自测试过,轻松解决问题!
方法二:控制器Controller中加入一段数据绑定代码
/*将字符串转换为Date类
@InitBinder
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));
}*/
方法三:实现一个全局日期类型转换器并进行配置
设置日期转换类
package nuc.ss.wlb.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;
public class CustomDateEdtor implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) {
// TODO Auto-generated method stub
//转换日期格式
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
并在spingMVC配置文件进行配置
<!-- 配置全局日期转换器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="nuc.ss.wlb.core.web.CustomDateEdtor"/>
</property>
</bean>
方法四:jsp页面配置或Freemark中配置
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:formatDate value="${job.jobtime }" pattern="yyyy-MM-dd HH:mm:ss"/>
方法一:实体类中加日期格式化注解
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
如上,在对应的属性上,加上指定日期格式的注解,本人亲自测试过,轻松解决问题!
方法二:控制器Controller中加入一段数据绑定代码
/*将字符串转换为Date类
@InitBinder
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));
}*/
方法三:实现一个全局日期类型转换器并进行配置
设置日期转换类
package nuc.ss.wlb.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;
public class CustomDateEdtor implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) {
// TODO Auto-generated method stub
//转换日期格式
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
并在spingMVC配置文件进行配置
<!-- 配置全局日期转换器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="nuc.ss.wlb.core.web.CustomDateEdtor"/>
</property>
</bean>
方法四:jsp页面配置或Freemark中配置
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:formatDate value="${job.jobtime }" pattern="yyyy-MM-dd HH:mm:ss"/>
0 0
- spring mvc开发接收日期字段表单提交,自动转换成Date类型报错,解决办法
- spring mvc开发接收日期字段表单提交,自动转换成Date类型为什么报错?
- spring mvc开发接收日期字段表单提交的问题
- spring mvc接收表单中的特殊类型字段
- springmvc提交表单日期用Date接收
- 关于springmvc怎么自动把前台string类型日期字段转换成date类型
- Spring MVC 3 表单提交时,自动将各输入字段封装成实体Bean
- 将Date日期类型的字段转换成json字符串
- springmvc使用实体类接收表单提交数据中含有String类型对应Date类型的不匹配报错400时如何处理
- spring mvc 将页面string类型日期转换为后台Date日期
- spring mvc提交日期类型参数
- spring mvc提交日期类型参数
- spring mvc提交日期类型参数
- 让spring mvc controller转换date类型
- SpingMvc配置string类型自动转换date日期类型
- spring mvc 表单提交
- spring mvc 表单提交
- Spring MVC提交表单
- 使用acorn对JavaScript代码进行解析。
- java学习之路之接口(4)--代理设计模式(代码)
- C# Lambda表达式
- CUDA Pro Tip:Write Flexible Kernels with Grid-Stride Loops
- Packagist 镜像使用方法
- spring mvc开发接收日期字段表单提交,自动转换成Date类型报错,解决办法
- Struts 03
- 手势锁 在本app中使用
- 关于Oracle与MySQL官方jdbc的一些区别
- java进行对象初始化的过程
- Markdown主要语法规则
- 遗忘---零散知识点2
- 4-5 求自定类型元素的最大值 (10分)
- Redis-zset (有序集合)