【问题标题】:Hibernate validator: how to handle rollover? (28/14/2009 becomes 28/2/2010)Hibernate 验证器:如何处理翻转? (2009 年 14 月 28 日变为 2010 年 2 月 28 日)
【发布时间】:2010-12-09 16:02:24
【问题描述】:

我正在使用休眠验证器来验证我的表单。 我有一个“问题”,即第 14 个月的第 9 号成为明年的第二个月。 (只是一个场景示例)。

我想知道如何防止默认转换,而是为其显示自定义错误消息。

如果我的自定义编辑器抛出 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 的绑定存在一些问题。

【问题讨论】:

    标签: spring-mvc hibernate-validator


    【解决方案1】:

    此行为由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); 
    

    【讨论】:

    • @jack:实际发生了什么——翻转还是IllegalArgumentException
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    相关资源
    最近更新 更多