2012年8月26日 星期日

[Spring] Type not matched when String is "" and is tried to convert to date

ERROR MESSAGE
Type not matched when String is "" and is tried to convert to date


Solution

因為 form 中的 date 為 "" (空字串) 因為要轉成 Date 時會出錯。

@InitBinder
protected void initializeBinder (WebDataBinder binder)  {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd" );
    dateFormat.setLenient(false);
    CustomDateEditor dateEditor = new CustomDateEditor(dateFormat, true);

    binder.registerCustomEditor(Date. class, dateEditor);
    binder.registerCustomEditor(ModelEntity.class, customPropertyEditor);
}

沒有留言:

張貼留言