Java WebDataBinder自定义日期编辑器不支持';行不通
我试图在spring的MVC WebDataBinder中注册自定义日期编辑器,使spring解析我的自定义日期fomat(确切地说,它是ISO格式)。 我成功地实现了CustomWebBindingInitializerJava WebDataBinder自定义日期编辑器不支持';行不通,java,spring,spring-mvc,Java,Spring,Spring Mvc,我试图在spring的MVC WebDataBinder中注册自定义日期编辑器,使spring解析我的自定义日期fomat(确切地说,它是ISO格式)。 我成功地实现了CustomWebBindingInitializer public static class CustomWebBindingInitializer implements WebBindingInitializer { @Override public void initBinder(WebDataBinde
public static class CustomWebBindingInitializer implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
CustomDateEditor dateEditor = new CustomDateEditor(new ISODateFormat(), true);
webDataBinder.registerCustomEditor(Date.class, dateEditor);
}
}
Spring正在使用我的编辑器并成功解析日期,但未绑定nontheless date字段,因此请求出现以下错误:
"org.springframework.validation.BindException",,"defaultMessage":"Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'from'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: \"2016-08-01T10:35:04.126Z\""
值得注意的是:
当我将默认的spring格式MM/DD/YYYY与我的自定义编辑器一起使用时,我得到了相同的错误,这意味着spring使用的是我的编辑器而不是默认编辑器
当我使用格式为MM/DD/YYYY的默认spring解析器时,一切正常,日期被绑定,这是显而易见的
有人有相同的问题吗?将格式添加到
registerCustomEditor
并尝试:
SimpleDateFormat format = new SimpleDateFormat("Required format");
webDataBinder.registerCustomEditor(Date.class, dateEditor, new CustomDateEditor(format, true));
通过使用PropertyEditorSupport而不是中的CustomDateEditor解决
使用所需的日期格式创建
SimpleDateFormat
,如“yyyy-MM-dd”
使用所需的dateForat
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));