Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc SpringMVC3:CustomDateEditor正在工作,但存在验证错误(BindingResult)_Spring Mvc_Spring Mvc Initbinders - Fatal编程技术网

Spring mvc SpringMVC3:CustomDateEditor正在工作,但存在验证错误(BindingResult)

Spring mvc SpringMVC3:CustomDateEditor正在工作,但存在验证错误(BindingResult),spring-mvc,spring-mvc-initbinders,Spring Mvc,Spring Mvc Initbinders,当我提交带有空日期的表单时(如生成的网页中的日期),我遇到了验证错误问题: Failed to convert property value of type java.lang.String to required type java.util.Date for property dateFin; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: ""

当我提交带有空日期的表单时(如生成的网页中的日期),我遇到了验证错误问题:

Failed to convert property value of type java.lang.String to required type 
java.util.Date for property dateFin; nested exception is 
java.lang.IllegalArgumentException: Could not parse date: Unparseable date: ""
我的控制器如下所示:

    @Controller
    @SessionAttributes
    @Lazy
    public class MyController extends AbstractMVPAction {
        @RequestMapping(value = "/secured/cp/saveProgram")
        public String enregistrerProgramme(@ModelAttribute Program program,
                 BindingResult bindingResult, ModelMap model){
            if(bindingResult.hasErrors()){
                model.put("program", program);
                return "/secured/cp/showProgram"
            }else{
                // ... saves the programme
                model.put("program", null);
                return "/secured/cp/backToOtherPage"
            }
        }
        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Date.class, new CustomDateEditor(
                    new SimpleDateFormat("dd/MM/yyyy"), false));
        }
    }
当我调试我的方法时,我可以看到我的对象很好,我所做的修改得到了很好的报告,日期为null,但是bindingResult.hasErrors()返回true,根据我的说法,它不应该返回true

我以前在程序对象中有一些验证注释和@Valid注释,但我将它们全部删除,仍然存在问题

我读过很多类似的问题,每次解决方案都是@InitBinder/CustomDateEditor。 所以它就在那里,我想它正在工作,日期是以我想要的方式显示的(在我添加它之前不是这样的),如果它不是空的,我可以提交它们


提前感谢,我开始发疯了…

您构建了一个CustomDateEditor,它明确禁止空字符串。选中,当布尔参数为false时,将空字符串传递给该编辑器会导致您看到的IllegalArgumentException

试试这个:

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("dd/MM/yyyy"), false));
    }

我确实需要一个晚上,我在早餐的时候想到了这样的事情。。。复制/粘贴是邪恶的!谢谢你的回答!