Java 对象时间戳绑定中的spring mvc字段错误
我使用的是spring 4.1.7.RELEASE 我无法将日期元素绑定到bean属性 我试过创建一个全局绑定器 我不知道我做错了什么Java 对象时间戳绑定中的spring mvc字段错误,java,spring,spring-mvc,binding,timestamp,Java,Spring,Spring Mvc,Binding,Timestamp,我使用的是spring 4.1.7.RELEASE 我无法将日期元素绑定到bean属性 我试过创建一个全局绑定器 我不知道我做错了什么 @ControllerAdvice public class CommonBindingInitializer { private static final String DATE_FORMAT = "yyyy-MM-dd"; @InitBinder public void registerCustomEditors(WebDataBin
@ControllerAdvice
public class CommonBindingInitializer {
private static final String DATE_FORMAT = "yyyy-MM-dd";
@InitBinder
public void registerCustomEditors(WebDataBinder binder, WebRequest request) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, request.getLocale());
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
binder.registerCustomEditor(java.util.Date.class, null, new CustomDateEditor(dateFormat, true));
binder.registerCustomEditor(Timestamp.class, null, new CustomDateEditor(dateFormat, true));
}
}
但这并不能解决我的问题。在我的控制器中,我总是出现以下错误:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'searchCriteria' on field 'dateCreation': rejected value [2015-09-16];
codes [typeMismatch.searchCriteria.dateCreation,typeMismatch.dateCreation,typeMismatch.java.sql.Timestamp,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [searchCriteria.dateCreation,dateCreation]; arguments [];
default message [dateCreation]];
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'dateCreation';
nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.sql.Timestamp]
for property 'dateCreation':
PropertyEditor [org.springframework.beans.propertyeditors.CustomDateEditor] returned inappropriate value of type [java.util.Date]]
你能告诉我怎么了吗
多谢各位 找到一个解决方案
正如拉尔夫在这篇评论中所说,这是非常清楚的
CustomDateEditor将字符串转换为java.util.Date(而不是java.sql.Timestamp)
通过重写CustomDateEditor并实例化一个新的时间戳(而不是setAsText方法中的java.util.date),我完成了自己的属性编辑器类。而且效果很好
希望它能帮助别人。找到解决方案
正如拉尔夫在这篇评论中所说,这是非常清楚的
CustomDateEditor将字符串转换为java.util.Date(而不是java.sql.Timestamp)
通过重写CustomDateEditor并实例化一个新的时间戳(而不是setAsText方法中的java.util.date),我完成了自己的属性编辑器类。而且效果很好
希望它能帮助别人