[英]How to bind CustomDateEditor to all Date fields in Springframework?
I am having a dataBind which is having few attributes alongwith a list of bean and one of the attribute of the bean is of type Date. 我有一个dataBind,它具有几个属性以及一个bean列表,并且bean的属性之一是Date类型。 Now i would like to add the customDateEditor to this date field. 现在,我想将customDateEditor添加到此日期字段。
My Databind goes like this: 我的数据绑定是这样的:
public class myDataBind{
/*
some attributes here
*/
List myList = new ArrayList(); // List of myBean
/*
accessor and mutators here
*/
}
public class myBean{
/*
some attributes here
*/
private Date fromDate = null;
private Date toDate = null;
/*
accessor and mutators here
*/
}
and in my Controller i am having 在我的控制器中
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
super.initBinder(request, binder);
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"){{ setLenient(false);}},false)); // Date.class is java.sql.Date.class
}
But still i am getting the error Field error in object 'command' on field 'myList[0].fromDate': rejected value [2009-05-27]; 但是我仍然在字段'myList [0] .fromDate'的对象'command'中收到错误字段错误:拒绝的值[2009-05-27]; codes [typeMismatch.command.myList[0].fromDate,typeMismatch.command.myList.fromDate,typeMismatch.myList[0].fromDate,typeMismatch.myList.fromDate,typeMismatch.fromDate,typeMismatch.java.sql.Date,typeMismatch]; 代码[typeMismatch.command.myList [0] .fromDate,typeMismatch.command.myList.fromDate,typeMismatch.myList [0] .fromDate,typeMismatch.myList.fromDate,typeMismatch.fromDate,typeMismatch.java.sql.Date,typeMismatch] ; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [command.myList[0].fromDate,myList[0].fromDate]; 参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[command.myList [0] .fromDate,myList [0] .fromDate]; arguments []; 参数[]; default message [myList[0].fromDate]]; 默认消息[myList [0] .fromDate]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.sql.Date] for property 'myList[0].fromDate'; 默认消息[未能将属性'myList [0] .fromDate'的类型[java.lang.String]的属性值转换为必需的类型[java.sql.Date]; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.sql.Date] for property 'fromDate': no matching editors or conversion strategy found] 嵌套的异常为java.lang.IllegalArgumentException: 无法将属性“ fromDate”的[java.lang.String]类型的值转换为所需的[java.sql.Date]类型:未找到匹配的编辑器或转换策略]
Please let me know, which step i am missing. 请让我知道,我错过了哪一步。
我不确定100%,但是:您是否需要调用super.initBinder(request,活页夹)?
It's quite a while back since I used Spring's validation framework. 自从我使用Spring的验证框架以来,已经有一段时间了。 By looking at your code and the details you provided I didn't find any discrepancies. 通过查看您的代码和您提供的详细信息,我没有发现任何差异。 The only difference I noted when looking at my code was that I first created the CustomDateEditor, registered it with the binder and then called the super.initBinder. 在查看代码时,我注意到的唯一区别是,我首先创建了CustomDateEditor,将其注册到活页夹,然后将其命名为super.initBinder。
Just for curiosity, did you try something like this 只是出于好奇,您尝试过这样的事情吗
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"){{ setLenient(false);}},false));
super.initBinder(request, binder);
}
In the error it mentions 'java.sql.Date' - in both your custom property editory and you bean class is the Date class referred to java.sql.Date or java.util.Date? 在错误中,它提到了“ java.sql.Date”-在您的自定义属性editory和bean类中,Date类都引用了java.sql.Date还是java.util.Date?
Ie have you imported the intended Date class in all places? 即您是否已在所有地方导入了预期的Date类?
CustomDateEditor
can only be used for java.util.Date
! CustomDateEditor
只能用于java.util.Date
!
This is documented in the class's documentation . 这在类的文档中进行了记录 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.