2010-12-09 46 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