我正在使用hibernate验证器来验证我的表单。我遇到了一个“问题”,第14个月的第9个月变成了第二年的第2个月。(只是一个场景的示例)。
我想知道如何阻止默认的转换,而是显示一个自定义的错误消息。
有没有人知道如果我的自定义编辑器抛出一个IllegalArgumentException,我如何显示一条合适的消息?
@InitBinder
public void initBinder(WebDataBinder binder) {
CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true);
binder.registerCustomEditor(Date.class, editor);
}我注册了一个customEditor,因为spring-portlet-mvc在绑定方面有一些问题。
发布于 2010-12-10 00:21:43
此行为由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); https://stackoverflow.com/questions/4400291
复制相似问题