2016-09-01 13 views
1

我想在春天的MVC WebDataBinder中註冊自定義日期編輯器,使春天解析我的自定義日期fomat(確切地說,它是ISO格式)。 我通過實現CustomWebBindingInitializer來實現這一目標。WebDataBinder自定義日期編輯器不工作

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

春天是使用我的編輯器,併成功地解析日期,但nontheless日期字段不獲取綁定,我得到以下錯誤的請求:

"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\"" 

值得一提的是: 當我使用默認彈簧的格式MM/DD/YYYY與我的自定義編輯器我得到相同的錯誤,這意味着春天使用我的編輯器,而不是默認的編輯器。

當我使用 默認春天的解析器與格式MM/DD/YYYY一切工作和日期獲取綁定,這是顯而易見的,當然

任何人都有同樣的問題?

回答

1

添加格式registerCustomEditor和嘗試:

SimpleDateFormat format = new SimpleDateFormat("Required format"); 
webDataBinder.registerCustomEditor(Date.class, dateEditor, new CustomDateEditor(format, true)); 
相關問題