2010-12-09 45 views
1

我正在使用hibernate驗證器來驗證我的表單。 我有第14個月的第9個月成爲明年第2個月的「問題」。 (僅僅是一個場景的例子)。Hibernate驗證器:如何處理翻滾? (28/14/2009變成28/2/2010)

我想知道如何防止默認轉換,而是顯示自定義錯誤消息。

有沒有人也知道如果我的自定義編輯器拋出IllegalArgumentException異常,我可以顯示一個appropenate消息?

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

我註冊了一個自定義編輯器,因爲spring-portlet-mvc在綁定時遇到了一些問題。

回答

2

此行爲是由DateFormat.setLenient()控制,並沒有任何與驗證(與setLentient(false)它在結合階段產生一個類型不匹配錯誤):

DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
df.setLenient(false); 
CustomDateEditor editor = new CustomDateEditor(df, true); 
binder.registerCustomEditor(Date.class, editor); 
+0

@jack:實際發生的 - 翻轉或`IllegalArgumentException` ? – axtavt 2010-12-10 13:08:03