Java – How to bind CustomDateEditor to all Date fields in Springframework

javaspring

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. Now i would like to add the customDateEditor to this date field.
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]; codes [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]; arguments []; default message [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'; 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]

Please let me know, which step i am missing.

Best Answer

I'm not 100% sure, but: do you need to call super.initBinder(request, binder)?