ValidationMessages.properties文件之间的冲突 [英] Conflict between ValidationMessages.properties files

查看:109
本文介绍了ValidationMessages.properties文件之间的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来收集公共库中的所有验证约束。在jar的根目录中,我放了一个 ValidationMessages_it.properties 文件。
一切正常,如果我将这个库放入jsf 2 war项目中,所有验证消息都会正确显示。
但是,如果我在war工件中放入另一个 ValidationMessages_it.properties ,则会出现问题。在这种情况下,会显示一个{library.message_key}字符串。

I use to collect all my validation constraints in a common library. In the root of the jar I put a ValidationMessages_it.properties file. All works fine, if I put this library into a jsf 2 war project all validation messages are shown correctly. However a problem arise if I put another ValidationMessages_it.properties in the war artifact too. In this case a {library.message_key} string is shown.

我认为Bean Validation在战争中找到了正确的属性文件,并没有考虑到库中的那个。
如何解决?

I think Bean Validation find the right property file in the war and does not take into account that in the library. How can I solve?

编辑

尽量澄清我的配置:

我有一个包含自定义约束的库commons.jar。为了设置这些约束的消息,我在这个库的根目录中添加了ValidationMessages_it.properties

I have a library, commons.jar, that contains custom constraints. In order to set messages for these constraints I've added a ValidationMessages_it.properties in the root of this library

commons.jar
    |
    + library
    |   |
    |   + CustomConstraint.class
    |
    + ValidationMessages_it.properties

Validation_it.properties:

Validation_it.properties:

library.custom=Questo è l'errore di cui parlavo

CustomConstraint.java:

CustomConstraint.java:

@Pattern( regexp = "[a-z]", message = "{library.custom}" )
@Constraint( validatedBy = {} )
@Documented
@Target( { ElementType.METHOD, ElementType.FIELD } )
@Retention( RetentionPolicy.RUNTIME )
public @interface CustomConstraint {
    String message() default "C'è un errore";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

PS: 注意消息密钥是在@Pattern注释而不是message(),这似乎是一个错误,否则它永远不会工作!

之后我想用这个我的网络应用程序项目中的commons.jar(jsf / mojarra 2.1)。一切正常。显示的错误消息是Questoèl'erroredi cui parlavo

After that I want to use this commons.jar in my web app project (jsf/mojarra 2.1). All works fine. The displayed error message is Questo è l'errore di cui parlavo

但现在假设我在我的webapp中定义了新的验证约束,所以我想要通过在WEB-INF / classes文件夹中添加ValidationMessages_it.properties来为这些约束提供翻译。在这种情况下,显示的错误消息是 {library.custom}

But now suppose I define new validation constraints in my webapp, so I want to supply translations for these constraints by adding a ValidationMessages_it.properties in WEB-INF/classes folder. In this case the displayed error message is {library.custom}

所以我认为BV(或jsf?)在战争并没有考虑在commons.jar中。它没有在位于WEB-INF / classes文件夹中的ValidationMessages_it.properties中找到密钥 library.custom ,因此从字面上返回 {library.custom}

So I think that BV (or jsf?) find the bundle in the war and does not take into account that in the commons.jar. It does not find the key library.custom in the ValidationMessages_it.properties that resides in the WEB-INF/classes folder thus return {library.custom} literally.

编辑2

为了更好地理解哈代的回答,我又打开了另一个问题验证我的假设是否正确:共享库中的Bean验证限制

In order to better understand the Hardy's answer I have opened another question to verify if my assumptions are right: Bean Validation constraints in a shared library

编辑3

根据上述问题,我的包结构似乎是正确的。我上传了一个简单的网络应用程序来显示问题:

Based on aforementioned question my package structure seems correct. I uploaded a simple web app to show the problem:

  • a war artifact that can be deployed in a java EE 6 compliant application server
  • a source zip that contains two maven projects, one for library and one for webapp

我测试了Glassfish 3.1.2,JBoss AS 7.1.1,Geronimo 3.0.0中的webapp

I tested the webapp in Glassfish 3.1.2, JBoss AS 7.1.1, Geronimo 3.0.0

Glassfish和Jboss具有相同的行为。在Geronimo中,它的工作效果更好。

Glassfish and Jboss have the same behavior. In Geronimo it works a little bit better.

推荐答案

我认为你的案例中的解决方案是提到的 AggregateResourceBundleLocator 。但是,属性文件的名称不能相同。内部 ResourceBundle#getBundle 被调用,它返回一个 ResourceBundle 。没有组合/合并具有相同名称的属性文件的概念。

I think the solution in your case is the mentioned AggregateResourceBundleLocator. However, you cannot have the same name for the property files. Internally ResourceBundle#getBundle is called which does return a single ResourceBundle. There is no concept of combining/merging properties files with the same name.

编辑1

关于标准的做法 - 不幸的是有没有。 Bean Validation 1.1(hibernate.onjira.com/browse/BVAL-252)存在一个未解决的问题,以解决提供约束库的问题,但目前还没有任何决定,消息插值也需要解决。也许你对它应该如何运作有所了解。如果是,请向专家组提出您的建议。检查beanvalidation.org

Regarding a standard way of doing it - unfortunately there is none. There is an open issue for Bean Validation 1.1 (hibernate.onjira.com/browse/BVAL-252) to address the ability to provide constraint libraries, but there is nothing decided yet and the message interpolation would needs addressing as well. Maybe you have an idea on how it should work. If so provide your suggestion to the expert group. Check beanvalidation.org

这篇关于ValidationMessages.properties文件之间的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆