springmvc关于前台日期作为实体类对象参数类型转换错误
页面报错:

后台错误:
Field error in object 'user' on field 'birthday': rejected value [2013-06-24]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2013-06-24'; nested exception is java.lang.IllegalArgumentException]
解决方案1:在对应的实体类属性上加入 @DateTimeFormat(pattern = "yyyy-MM-dd")

解决方案2:不使用 <mvc:annotation-driven/>注解
使用 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 注解驱动配置
在对应的实体类属性上加入 @DateTimeFormat(pattern = "yyyy-MM-dd")
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService">
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
</property>
</bean>
</property>
</bean>
3、使用 @InitBinder注解,注册一个父类Controller(BaseController),其他Controller去继承它
Springmvc配置文件 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
public class BaseController {
@InitBinder
public void initBinder(WebDataBinder binder) {
/**
* 第一种方式:使用WebDataBinder注册一个自定义的编辑器,编辑器是日期类型
* 使用自定义的日期编辑器,日期格式:yyyy-MM-dd,第二个参数为是否为空 true代表可以为空
*/
binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true));
}
}
或者使用下面的方式
public class BaseController {
@InitBinder
public void initBinder(WebDataBinder binder) {
/**
* 方式二:使用WebDataBinder注册一个自定义的编辑器,编辑器是日期类型
* 使用属性编辑器实现:重载setAsText,getAsText
*/
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public String getAsText() {
return new SimpleDateFormat("yyyy-MM-dd")
.format((Date) getValue());
}
@Override
public void setAsText(String text) {
try {
setValue(new SimpleDateFormat("yyyy-MM-dd").parse(text));
} catch (Exception e) {
e.printStackTrace();
setValue(null);
}
}
});
}
}
springmvc关于前台日期作为实体类对象参数类型转换错误的更多相关文章
- @NamedEntityGraphs --JPA按实体类对象参数中的字段排序问题得解决方法
JPA按实体类对象参数中的字段排序问题得解决方法@Entity @Table(name="complaints") @NamedEntityGraphs({ @NamedEntit ...
- jpa @Query()参数设置,:冒号方式、?NO.问号方式、实体类对象参数设置
一.service层事务(update/delete) @Transactional(rollbackFor = Exception.class) 二.@Query()参数设置 ?x 和:XX不能混 ...
- NSDictionary转化为实体类对象
方法一: 使用objective-c NSObject自带的方法 setValuesForKeysWithDictionary:dict 作用是: 如果NSDictionary中的key和实体类对象的 ...
- java 获取实体类对象属性值的方法
在java中我们要获得实体类对象的属性,一般情况是将实体类中的属性私有化,然后再对外提供get()与set()方法,然后再获取实体类对象的属性的时候先把对象new出来,再用变量名.get()的方法得到 ...
- Hibernate_day02--课程安排_主键生成策略_对实体类crud操作_实体类对象状态
Hibernate_day02 上节内容 今天内容 实体类编写规则 Hibernate主键生成策略 实体类操作 对实体类crud操作 添加操作 根据id查询 修改操作 删除操作 实体类对象状态(概念) ...
- Mybaits 源码解析 (八)----- 全网最详细,没有之一:结果集 ResultSet 自动映射成实体类对象(上篇)
上一篇文章我们已经将SQL发送到了数据库,并返回了ResultSet,接下来就是将结果集 ResultSet 自动映射成实体类对象.这样使用者就无需再手动操作结果集,并将数据填充到实体类对象中.这可大 ...
- 使用fastjson 进行jsonObject转实体类对象
使用fastjson 进行jsonObject转实体类对象 1 <dependency> 2 <groupId>com.alibaba</groupId> 3 ...
- solr搜索结果转实体类对象的两种方法
问题:就是把从solr搜索出来的结果转成我们想要的实体类对象,很常用的情景. 1.使用@Field注解 @Field这个注解放到实体类的属性[字段]中,例如下面 public class User{ ...
- SpringBoot实体类对象和json格式的转化
1.引入maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson ...
随机推荐
- sqoop1.9.7安装和使用
安装1.下载sqoop1.9.7.地址: http://www.apache.org/dyn/closer.lua/sqoop/1.99.72.解压sqoop ,并配置环境变量 ~/.bash_pro ...
- eclipse禁用svg文件Validation
1.打开window>preferences>validation找到xml validator 2.点击xml validator最右侧的按钮打开xml校验规则窗口,选中exclude ...
- 通用的contain函数
用来检测节点所属关系:document.documentElement.contains(document.body) function contains(refNode, otherNode) {i ...
- display:none,float小秘密
一个元素不管是块元素还是行内元素 在添加了 display:none 之后,就变成了不可见的块元素,可以给他添加长度和高度 在float之后内联元素也会隐性成为 inline-block ...
- jQuery选择器(属性过滤选择器)第六节
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- PHP静态化技术
很多框架的模板引擎都有页面静态化的功能 目的是为了优化网站运行时间 静态化分两种 纯静态和伪静态 一. 纯静态 纯静态展示的是实实在在的静态页面 运行PHP程序 判断是否存在静态页 如果存在 展示 ...
- css-子div设置margin-top影响父div
父元素的第一个子元素的上边距margin-top如果碰不到有效的border或者padding.就会不断一层一层的找自己父元素,祖先元素,所有需要在父元素设置border,或者padding
- python的学习之路day1
软件:python3.pycharm开发工具 python的开始:print("hello world") 注意:python3需要加上() 1.变量是什么:在程序运行过程中它的值 ...
- Unity中的Mono & Linux上编译Mono的流程
前段时间编译了一下Unity的Mono,看了很多相关的文章,也遇到很多新坑.所以来总结一下,加深自己对Mono的理解 为什么Unity可以跨平台运行呢 通常Unity的脚本有C#.JS.Boo.不过现 ...
- Gitpage + hexo(3.0以上)搭建博客
大半天,一边折腾,一边查找各种文档,写出的这篇文档,不知道有没有把程序表示得足够简明,有不足之处望指明. 前提:已安装好nodeJS和git. 桌面右击进入gitbash,输入npm install ...