Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将CustomDateEditor绑定到Springframework中的所有日期字段?_Java_Spring - Fatal编程技术网

Java 如何将CustomDateEditor绑定到Springframework中的所有日期字段?

Java 如何将CustomDateEditor绑定到Springframework中的所有日期字段?,java,spring,Java,Spring,我有一个数据绑定,它有几个属性和一个bean列表,bean的一个属性是Date类型。现在,我想将customDateEditor添加到此日期字段。 我的数据绑定如下: public class myDataBind{ /* some attributes here */ List myList = new ArrayList(); // List of myBean /* accessor and mutators here */ }

我有一个数据绑定,它有几个属性和一个bean列表,bean的一个属性是Date类型。现在,我想将customDateEditor添加到此日期字段。
我的数据绑定如下:

    public class myDataBind{
       /* 
    some attributes here
    */

List myList = new ArrayList();  // List of myBean

/*
  accessor and mutators here
*/
    }


在我的控制器里我有

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    
}

但我还是得到了错误 字段“myList[0]”上的对象“command”中存在字段错误。fromDate:拒绝值[2009-05-27];代码[TypeMatch.command.myList[0]。fromDate,TypeMatch.command.myList.fromDate,TypeMatch.myList[0]。fromDate,TypeMatch.myList.fromDate,TypeMatch.fromDate,TypeMatch.java.sql.Date,TypeMatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[command.myList[0]。fromDate,myList[0]。fromDate];参数[];默认消息[myList[0]。fromDate]];默认消息[未能将[java.lang.String]类型的属性值转换为属性'myList[0].fromDate'所需的[java.sql.Date]类型;嵌套异常为java.lang.IllegalArgumentException:无法将[java.lang.String]类型的值转换为所需的[java.sql.Date]类型对于属性“fromDate”:未找到匹配的编辑器或转换策略]


请告诉我,我遗漏了哪一步。

我不是100%确定,但是:您需要调用super.initBinder(request,binder)吗?

我使用Spring的验证框架已经很久了。通过查看您的代码和您提供的详细信息,我没有发现任何差异。在查看代码时,我注意到的唯一区别是,我首先创建了CustomDateEditor,在活页夹中注册它,然后调用super.initBinder

只是出于好奇,你试过这样的东西吗

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);
}

在错误中,它提到了“java.sql.Date”-在自定义属性editory和bean类中,Date类是指java.sql.Date还是指java.util.Date


也就是说,您是否已在所有位置导入了预期的日期类?

CustomDateEditor
只能用于
java.util.Date

这一点记录在课堂上

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);
}