博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Failed to convert from type java.lang.String to type java.util.Date for value解决办法
阅读量:2387 次
发布时间:2019-05-10

本文共 1069 字,大约阅读时间需要 3 分钟。

在springMVC中如果表单属性的类型是日期类型时,从页面绑定字符串数据会出错,Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'expert.birthdate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'birthdate': no matching editors or conversion strategy found

在用SpringMVC整合mybatis的时候,在controller中调用service进行保存数据的操作,遇到了Failed to convert from type .lang.String to type java.util.Date for value………这个错误,然后发现有个字段在Entity中的类型是Date的,然后表单传过来的数据时String类型的,这时就会出现这样的问题,类型转换错误!

解决方法

1.控制器继承 extends SimpleFormController

2.重写initBinder方法

要加一个@InitBinder,查了一下资料,貌似是用于初始化数据的时候,进行数据类型转换,把String类型转为Date类型,这样就不会报错了。

在对应的Controller类中加上如下代码,使用注解,就可以解决问题。

//用于初始化数据的时候,进行数据类型转换,把String类型转为Date类型
@InitBinder
public void initBinder(WebDataBinder binder){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

}

注意SimpleDateFormat日期格式与页面日期格式要一致!

转载地址:http://jjiab.baihongyu.com/

你可能感兴趣的文章
Adobe ColdFusion Unspecified Directory Traversal Vulnerability
查看>>
Share:A File Checksum Integrity Verifier utility
查看>>
LDAP User Authentication On CentOS 5.x
查看>>
Cpanel PHP Restriction Bypass Vulnerability 0day
查看>>
Web服务器捉虫速记
查看>>
INITTAB详解
查看>>
Black Hat USA 2011: Alexander Polyakov - CTO - ERPScan
查看>>
PHP端口复用的利用
查看>>
Hyperion 11 安装
查看>>
WEBSHELL新型玩法
查看>>
weblogic node maanger 远程命令执行漏洞
查看>>
信息与网络安全相关参考资料的下载
查看>>
Remote IIS 5.x and IIS 6.0 Server Name Spoof
查看>>
RACI 责任分配矩阵
查看>>
利用CSVDE和DSADD实现AD帐号批量导入导出
查看>>
oracle用户创建及权限设置
查看>>
vBulletin 4.X后台拿shell
查看>>
Windows Server 2008 R2之十全局编录服务器(GC)
查看>>
Exchange域名重写,实现SMTP地址共享
查看>>
我眼里的Exchange 2010 之:1—DAG
查看>>