今天在完成项目的时候遇到了下面的异常信息:

04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [cms] in context with path [/cms] threw exception [Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 6 errors
Field error in object 'carContractSearchBox' on field 'createEndDate': rejected value []; codes [typeMismatch.carContractSearchBox.createEndDate,typeMismatch.createEndDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [carContractSearchBox.createEndDate,createEndDate]; arguments []; default message [createEndDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createEndDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'createEndDate': no matching editors or conversion strategy found]

先说明一下,我们的项目使用的是Spring MVC。相应的功能是一个简单的form表单查询功能,里面有一些日期字段的查询。

相应的解决办法为:

在对应的controller中增加属性编辑器:

@InitBinder
protected 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.
* <p>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报错。

Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found的更多相关文章

  1. spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'

    在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错: Failed to convert property value of type [java.lang.String] ...

  2. 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '

    没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...

  3. Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';

    springboot jdbc查询使用LocalDate报:Failed to convert value of type 'java.lang.String' to required type 'j ...

  4. Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFa ...

  5. 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';

    后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...

  6. SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tac ...

  7. 完美解决报错Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

    Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 首先这个错误的意思是 前台页面 ...

  8. Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'

    查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss 服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日 ...

  9. 前台传参数时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'

    springMVC action接收参数: org.springframework.validation.BindException: org.springframework.validation.B ...

随机推荐

  1. 前端见微知著番外篇:GIT舍我其谁?

    在上一篇中,我们讲到了利用纯UI的软件如何实现代码的提交.但是在MAC机器上,是没有turtoiseGit这类软件的,所以利用命令行的方式就是我们的首选了. 下面我们来描述两种主要的Git使用场景: ...

  2. 数据字典生成工具之旅(6):NVelocity语法介绍及实例

    本章开始将会为大家讲解NVelocity的用法,并带领大家实现一个简单的代码生成器. NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语 ...

  3. 异常检测算法--Isolation Forest

    南大周志华老师在2010年提出一个异常检测算法Isolation Forest,在工业界很实用,算法效果好,时间效率高,能有效处理高维数据和海量数据,这里对这个算法进行简要总结. iTree 提到森林 ...

  4. ASP.NET Web API Help Pages using Swagger

    Understanding the various methods of an API can be a challenge for a developer when building a consu ...

  5. .net程序员转行做手游开发经历(一)

    从辞职到自己开发游戏也有几个月的时间了,游戏也已经在AppStore上线了,我觉得我有必要写点东西,算是留下的一些记忆,也可以和广大博友分享下自己的创业经历,这可能不是一篇成功的创业经历,因为故事还在 ...

  6. 项目分布式部署那些事(2):基于OCS(Memcached)的Session共享方案

    在不久之前发布了一篇"项目分布式部署那些事(1):ONS消息队列.基于Redis的Session共享,开源共享",因为一些问题我们使用了阿里云的OCS,下面就来简单的介绍和分享下相 ...

  7. 初用protobuf-csharp-port

    下面这个用法是参照protobuf-csharp-port的官方wiki,参见: https://code.google.com/p/protobuf-csharp-port/wiki/Getting ...

  8. MATLAB如何定义函数

    自定义函数的途径:M文件函数(M file function)在线函数(Inline Function)匿名函数(Anonymous Function)1.M文件函数范例function c=myad ...

  9. android之广播(二)

    广播接受者不仅可以通过清单文件来向系统注册,也可以通过代码来注册.并且有的广播必须通过代码来注册广播接受者. 锁屏和解锁广播 电量改变广播 打开屏幕和关闭屏幕 这里将广播接收者写在服务里面 <? ...

  10. No Launcher activity found!

    已经研究Android有几天了,刚开始写的代码说安装成功,但是在AVD没有显示.左看代码,右看代码,总是没找到错误, <application android:allowBackup=" ...