简体   繁体   English

带有Spring和Hibernate的JSR-303混合错误消息

[英]JSR-303 with Spring and Hibernate mixed error messages

What I would like to do is have a standard error message that uses an annotation to fill in a value on the said error message. 我想要做的是有一个标准的错误消息,使用注释填写上述错误消息的值。 The code should help explain. 代码应该有助于解释。

I have a model POJO that has fields on it with JSR-303 annotations as such: 我有一个模型POJO,其中包含JSR-303注释的字段:

@Min(value = 1)
@Max(value = 9999)
private int someInt;

and PRESENTLY, I have two properties files: one to hold labels for my fields, and another to hold the actual messages: 现在,我有两个属性文件:一个用于保存我的字段的标签,另一个用于保存实际的消息:

label.someInt=Some Integer Field
Min="{0}" must be no less than {value}

Then I loop through stuff to build what I want: 然后我循环遍历构建我想要的东西:

for (FieldError a : result.getFieldErrors()) {
        boolean found = false;
        for (String b : a.getCodes()) {
            System.out.println(b);
            try {
                addErrorMessage(messageSource.getMessage(b,
                        new Object[] { new DefaultMessageSourceResolvable(
                            new String[] { "label." + a.getField() }
                        )},
                        null));
                found = true;
                break;
            } catch (Exception e){/* nom Exception */}
        }
        if (!found) {
            addErrorMessage(a.getDefaultMessage());
        }
    }

That's all fine and dandy, but I would like to be able to stick with the annotation-based way of doing things and add some sort of label on the fields. 这一切都很好,但我希望能够坚持基于注释的做事方式并在字段上添加某种标签。 For instance: 例如:

@Min(value = 1)
@Max(value = 9999)
@Label(value = "Some Integer Field")
private int someInt;

Then I could write a helper function that would just use the annotation to label the error message I have in the properties file. 然后我可以编写一个辅助函数,它只使用注释来标记我在属性文件中的错误消息。 I also need this to work with Spring-based validation errors such as typeMismatch. 我还需要这个来处理基于Spring的验证错误,比如typeMismatch。 Fun stuff, that. 有趣的东西,那。

Thanks in advance for your help! 在此先感谢您的帮助!

Have a look at this forum: http://forum.springsource.org/archive/index.php/t-83353.html 看看这个论坛: http//forum.springsource.org/archive/index.php/t-83353.html

or you can use the standard message keys supported by the DefaultMessageCodesResolver 或者您可以使用DefaultMessageCodesResolver支持的标准消息密钥

It looks like it is already implemented. 它看起来已经实现了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM