页面报错:

后台错误:

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. poj 1759 Garland

    Garland Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2365   Accepted: 1007 Descripti ...

  2. maximum shortest distance

    maximum shortest distance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  3. SQL查询多条不重复记录值简要解析【转载】

    转载http://hi.baidu.com/my_favourate/item/3716b0cbe125f312505058eb SQL查询多条不重复记录值简要解析2008-02-28 11:36 以 ...

  4. ASP.NET MVC中URL末尾斜杠的实现

    在网站的SEO优化中,通常都会涉及到URL结尾斜杠的问题. http://blog.sina.com.cn/s/blog_828e7ce40100srj1.html http://www.dengyo ...

  5. Java反射机制使用场景

    import java.io.*; import java.util.Properties; /*问题描述:存在一个主板--已经定义好,不想修改其代码,还想在主板上面增加一些其他功能? *问题解决方法 ...

  6. Git(3)----Eclipse上Git插件使用技巧

    转载:http://blog.csdn.net/qq_33066205/article/details/56675704 一_安装EGIT插件 http://download.eclipse.org/ ...

  7. 在moba游戏里面模拟实现绝地求生毒雾圈功能

    ---恢复内容开始--- 已经有很长一段时间没做项目了上半年大多数时间都荒废在王者荣耀,哈哈,S8赛季也上了王者,美滋滋 转回正题,公司需要开个新项目,需要把原有的moba游戏,改成类似绝地求生那玩法 ...

  8. MyEclipse10激活方法

    背景:因为以前一直使用的是myeclipse8.6版本,但因为版本太低有些功能不支持,于是想试用下myeclipse10.0版本,但是下载后发现需要激活,但在激活的过程中遇到了很多坑,于是便有了本文的 ...

  9. 休息,归类一下CSS初级的东西

    css基础的东西集中体现在了"磊盒子"这一个枯燥无味的东西上面,灵活的运用盒子的内外边距,浮动,定位以及一些基础的属性,将一个静态的页面变得磊出来,这是CSS基础的练习. 在css ...

  10. Python之作用域

    作用域测试例子: >>> a = 10 >>> def test(): ... a = 20 ... print a ... >>> a 10 & ...