解决springmvc关于前台日期作为实体类对象参数类型转换错误的问题
作者:jingxian 发布时间:2023-11-28 20:53:42
页面报错:
后台错误:
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);
}
}
});
}
}


猜你喜欢
- 一:SparkSQL1.SparkSQL简介Spark SQL是Spark的一个模块,用于处理结构化的数据,它提供了一个数据抽象DataFr
- 前言学习自定义view,想找点东西耍一下,刚好看到抖音的点赞效果不错,尝试一下。抖音效果: 话不多说,先上代码:public class L
- 本文实例为大家分享了java实现随机数生成器的具体代码,供大家参考,具体内容如下自己编的随机数生成器,比较简陋,功能也单一,当作练手。App
- 本篇文章介绍selenium 操作浏览器阅读目录浏览器最大化 前进,后退, 刷新截图操作模拟鼠标操作杀掉Windows浏览器进程浏览器最大化
- Json 是一个用于 Java 对象 和 Json 文本 相互转换的工具类。安装下载源码g
- 前言说到对集合去重处理,第一时间想到的肯定是Linq的Distinct扩展方式,对于一般的值类型集合去重,很好处理,直接list.Disti
- 继承ClassLoader并且重写findClass方法就可以自定义一个类加载器,具体什么是类加载器以及类加载器的加载过程与顺序下次再说,下
- 一、单例模式我们先来看看两种创建单例模式的示例代码。1、饿汉式 饿汉式创建单例模式是在程序里面直接初始化了一个对象实例:class
- 在不用Maven的时候,比如说以前我们用Ant构建项目,在项目目录下,往往会看到一个名为/lib的子目录,那里存放着各类第三方依赖jar文件
- 一、冻结列DataGridViewColumn.Frozen属性为true时,该列左侧的所有列被固定,横向滚动时固定列不随滚动条滚动而左右移
- 本文实例讲述了Android编程实现图片的浏览、缩放、拖动和自动居中效果的方法。分享给大家供大家参考,具体如下:Touch.java/**
- launch 是 CoroutineScope 的一个扩展函数,该方法在不阻塞当前线程的情况下启动新的协程,launch 里面的代码虽然有挂
- 在 WinForms 中,有时要执行耗时的操作,在该操作未完成之前操作用户界面,会导致用户界面停止响应。解决的方法就是新开一个线程,把耗时的
- 想必大家都知道,国内的Android应用基本都是免费的,那么开发者如何获得收入呢?应用中插入广告是一个比较常用的盈
- 本文实例总结了Android开发中的简单设置技巧。分享给大家供大家参考,具体如下:1开机图片:android-logo-mask.pngan
- 首先:看问题图,如下可以激活ide的网址很多,估计是个团队或者个人,直接买了全部产品的一年的有效期。而且还是会一直更新下去的。因为,后来我自
- 1 ArrayList在集合框架中,ArrayList是一个普通的类,实现了List接口,具体框架图如下:说明:ArrayList实现了Ra
- 一. 简单介绍一下Spring Boot世界惯例,在学习一个框架之前,我们需要了解一下这个框架的来历。下面我们引用一下百度百科的解释。Spr
- 本项目主要实现对汽车维修厂的信息化管理功能,主要包含三个角色:管理员,维修师傅,客户。实现的主要功能包含用户管理、配置管理、汽车管理、故障管
- 话不多说,请看代码:package com.lxj.demo;import java.io.BufferedReader;import ja