页面报错:

后台错误:

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关于前台日期作为实体类对象参数类型转换错误的更多相关文章

  1. @NamedEntityGraphs --JPA按实体类对象参数中的字段排序问题得解决方法

    JPA按实体类对象参数中的字段排序问题得解决方法@Entity @Table(name="complaints") @NamedEntityGraphs({ @NamedEntit ...

  2. jpa @Query()参数设置,:冒号方式、?NO.问号方式、实体类对象参数设置

    一.service层事务(update/delete) @Transactional(rollbackFor = Exception.class) 二.@Query()参数设置 ?x  和:XX不能混 ...

  3. NSDictionary转化为实体类对象

    方法一: 使用objective-c NSObject自带的方法 setValuesForKeysWithDictionary:dict 作用是: 如果NSDictionary中的key和实体类对象的 ...

  4. java 获取实体类对象属性值的方法

    在java中我们要获得实体类对象的属性,一般情况是将实体类中的属性私有化,然后再对外提供get()与set()方法,然后再获取实体类对象的属性的时候先把对象new出来,再用变量名.get()的方法得到 ...

  5. Hibernate_day02--课程安排_主键生成策略_对实体类crud操作_实体类对象状态

    Hibernate_day02 上节内容 今天内容 实体类编写规则 Hibernate主键生成策略 实体类操作 对实体类crud操作 添加操作 根据id查询 修改操作 删除操作 实体类对象状态(概念) ...

  6. Mybaits 源码解析 (八)----- 全网最详细,没有之一:结果集 ResultSet 自动映射成实体类对象(上篇)

    上一篇文章我们已经将SQL发送到了数据库,并返回了ResultSet,接下来就是将结果集 ResultSet 自动映射成实体类对象.这样使用者就无需再手动操作结果集,并将数据填充到实体类对象中.这可大 ...

  7. 使用fastjson 进行jsonObject转实体类对象

    使用fastjson 进行jsonObject转实体类对象   1 <dependency> 2 <groupId>com.alibaba</groupId> 3 ...

  8. solr搜索结果转实体类对象的两种方法

    问题:就是把从solr搜索出来的结果转成我们想要的实体类对象,很常用的情景. 1.使用@Field注解 @Field这个注解放到实体类的属性[字段]中,例如下面 public class User{ ...

  9. SpringBoot实体类对象和json格式的转化

    1.引入maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson ...

随机推荐

  1. 简单的一句sql

    表1 Id Name 1 张三 2 李四 3 王五 表二 Id Name1 Name2 1 1 2 2 2 1 3 2 3 4 1 3 我现在要查出结果如下: Id Name1 Name2 1 张三 ...

  2. 4本相见恨晚的Linux入门书籍

    有读者问可否推荐一些 Linux 入门书籍,刚好在知乎也看到类似的问题,几个零碎的命令难以在 Linux 环境中存活,如果要真正形成自己的知识体系,还是要靠阅读专业书籍来积累.Linux 对后端开发是 ...

  3. js之学习正则表达式

    看了掘金的一个作者写的JS正则表达式完整教程 受益匪浅,感谢作者的无私奉献.在此,做下笔记. 目录 0. 目录 1. 正则表达式字符匹配 1.1.字符组 1.2.量词 1.3.多选分支 1.4.案例分 ...

  4. 深入分析Android动画(一)

    动画的分类: ①View动画 View动画顾名思义其作用对象为View,包含平移.缩放.旋转.透明,这四类变化分别对应着Animation的子类TranlateAnimation.ScaleAnima ...

  5. layui数据表格以及传数据方式

    数据表格一: <div style="margin:0px; background-color: white; margin:0 10px;"> <blockqu ...

  6. CopyOnWriteArrayList并发容器

    CopyOnWriteArrayList并发容器 Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才 ...

  7. Unity 游戏框架搭建 (二十) 更安全的对象池

    上篇文章介绍了,只需通过实现IObjectFactory接口和继承Pool类,就可以很方便地实现一个SimpleObjectPool.SimpleObjectPool可以满足大部分的对象池的需求.而笔 ...

  8. details和summary可以对内容进行折叠

    使用<details>和<summary>元素 它可以在body的任意地方使用下面有一个小例子 <!DOCTYPE html> <html lang=&quo ...

  9. 利用C#来做ASP.NET的登陆页面

    一.新建一个数据库 新建一个access数据user.mdb. 新建一个user表,添加:UserId(文本类型)及Password(文本类型)两个字段.二.新建一个default.aspx文件. 在 ...

  10. mac环境下支持PHP调试工具xdebug,不需要建项目server

    先让php支持xdebug 方式一: https://xdebug.org/download.php 下载相应的xdebug  可以到http://xdebug.org/wizard.php 把php ...