博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
表单中字符串封装对象转换成Date出错
阅读量:7013 次
发布时间:2019-06-28

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

hot3.png

default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'createDate': no matching editors or conversion strategy found]

这里说明一下,我们的项目后台使用的是Spring MVC。在前端有个页面里有一个简单的form表单查询功能,表单里面有一些日期字段的查询,该日期字段是My97获取到时间值得

问题分析

表单中有属性,值为My97 Date值,获取到后,提交到后台,出现了上面的这一对异常信息。前端页面提交的时候参数检查了一下,看上去属性值都是对的,没什么问题。但是后端就是无法接收到参数。

问题排查

去百度了一下上面的异常信息,得出的解决方法,方案如下

解决方案

在对应的controller中增加属性编辑器,也就是在对应的Controller类中加上这个方法就可以了

@InitBinderprotected void initBinder(WebDataBinder binder) {    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}

注意这块的new CustomDateEditor(dateFormat, true)中的true,查看CustomDateEditor源码可以看到:

/** * Create a new CustomDateEditor instance, using the given DateFormat * for parsing and rendering. * 

The "allowEmpty" parameter states if an empty String should * be allowed for parsing, i.e. get interpreted as null value. * Otherwise, an IllegalArgumentException gets thrown in that case. * @param dateFormat DateFormat to use for parsing and rendering * @param allowEmpty if empty strings should be allowed */public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.exactDateLength = -1;}

当allowEmpty字段为true的时候form表单传递的值可以为空。否则会出现字符串解析为date报错。 所以这里最好是要设置一下为true,不然如果没有值,可能也报错了

转载于:https://my.oschina.net/u/2426199/blog/798273

你可能感兴趣的文章
产品经理技能树之 项目节点
查看>>
简析Android软键盘弹出时窗口上移的问题
查看>>
http长轮询&短轮询
查看>>
Android 应用换肤功能(白天黑夜主题切换)
查看>>
Linux编程操作知识整理(continued)
查看>>
2012.8.13 onEnter与触摸事件
查看>>
基于 HTML5 WebGL 的 3D 棉花加工监控系统
查看>>
[redis] 获得 database, key, value
查看>>
swift之mutating关键字
查看>>
Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的W...
查看>>
10个有趣的Javascript和CSS库
查看>>
ZiSync:跨平台局域网自同步工具
查看>>
使用 链接服务器执行SELECT、UPDATE、INSERT 或 DELETE 及其它命令
查看>>
Linux查看线程
查看>>
迭代三测试报告
查看>>
CSS display:none和visibility:hidden区别
查看>>
android应用开发简单理解
查看>>
解决eclipse存储git密码时Writing to secure store failed No password provided.错误
查看>>
eclipse连接android设备的问题
查看>>
localstorage 在各个浏览器下面的坑
查看>>