[英]spring mvc Field error in object timestamp binding
I'm using spring 4.1.7.RELEASE. 我正在使用Spring 4.1.7.RELEASE。
I can't bind date element to a bean property. 我不能将date元素绑定到bean属性。
I've tried creating a global binder. 我尝试创建一个全局活页夹。
I don't know what i'm doing wrong. 我不知道我在做什么错。
@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));
}
}
But this not solve my problem.In my controller I always have the error : 但这不能解决我的问题。在我的控制器中,我总是有错误:
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]]
Can you tell me what's wrong ? 你能告诉我怎么了吗?
Thank you. 谢谢。
Find a solution. 寻找解决方案。
As Ralph said in this comment Timestamp Spring conversion It's very clear. 就像Ralph在此评论中所说的, Timestamp Spring转换非常清楚。
CustomDateEditor convert a string to java.util.Date (and not to java.sql.Timestamp). CustomDateEditor将字符串转换为java.util.Date(而不是java.sql.Timestamp)。
I have done my own property editor class by overriting CustomDateEditor and instanciate a new Timestamp instead of java.util.date ine the setAsText Method. 我通过覆盖CustomDateEditor并实例化了一个新的Timestamp而不是setAsText方法中的java.util.date来完成自己的属性编辑器类。 And it works fine. 而且效果很好。
Hope it help someone. 希望它能帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.