Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx'
2017-03-21 11:47
911 查看
今天在完成项目的时候遇到了下面的异常信息:
先说明一下,我们的项目使用的是Spring MVC。相应的功能是一个简单的form表单查询功能,里面有一些日期字段的查询。
相应的解决办法为:
在对应的controller中增加属性编辑器:
注意这块的
当
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'
- Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found
- Spring 整合 Flex (BlazeDS)无法从as对象 到 Java对象转换的异常:org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Date' to required type 'java.sql.Timestamp' for property 'wfsj'; nested exception is java.lang.Ill
- Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for pr
- [置顶] 解决:Failed to convert value of type 'java.lang.String' to required type 'java.util.Date';
- Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for pr
- Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for pr
- 解决:Failed to convert value of type 'java.lang.String' to required type 'java.util.Date';
- Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching e
- 关于springMVC中:Failed to convert property value of type 'java.lang.String' to required type 'java.util
- java.lang.IllegalArgumentException: Cannot convert value of type [] to required type [] for property
- 配置Spring Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource]
- FAQ(69):java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required
- Failed to convert property value of type [java.lang.String] to required type [boolean] for property
- SpringMVC Failed to convert from type java.lang.String to type java.util.Date for value '2014-12-02'
- Failed to convert from type java.lang.String to type java.util.Date for value………解决办法
- spring Failed to convert property value of type 'java.lang.String' to required type 'int' for proper
- spring mvc 数据绑定报错 Failed to convert property value of type 'java.lang.String' to required type 'int'
- springmvc-- Failed to convert property value of type 'java.lang.String' to required type
- spring Failed to convert property value of type 'java.lang.String' to required type 'int' for proper