无法在Spring MVC中验证日期

4 validation date spring-mvc

我正在使用自定义编辑器将String转换为日期。我的代码在下面提到

@InitBinder
public void initBinder(WebDataBinder binder) {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));

}
Run Code Online (Sandbox Code Playgroud)

当我在“ yyyy-MM-dd”中输入日期时,将其插入。

但是当我输入空白时会引发异常

嵌套异常为java.lang.IllegalArgumentException:无法解析日期:无法解析的日期:“”

我无法验证相同的结果。

如果我给出其他格式,它也无法解析。

小智 5

实例化CustomDateEditor时,根据您的情况,第二个参数应为true。True允许空值。

CustomDateEditor(SpringSource)