从类路径中读取几个ValidationMessages.properties

lof*_*ses 3 java hibernate-validator properties-file

我在JSR303和特殊的Hibernate-Validator中遇到问题.

我想从属性文件中读取验证消息.此属性文件位于部署到servlet容器的war文件中.问题是,它没有被访问.我有以下项目结构.

  • project-web(战争档案)
    • project-web-core(项目-web.war的WEB-INF/lib中的jar文件)
      • hibernate-validator jar(项目web-core的maven依赖)
      • ValidationMessages.properties(在project-web-core.jar的src/main/resources中;访问此一个)
    • ValidationMessages.properties(在project-web.war的src/main/resources中; 访问此项)

如何访问ValidationMessages.properties?或者这不可能吗?

除此之外,如果Hibernate-Validator首先在战争中读取属性文件并且如果它找不到密钥,那么它将是完美的,然后从jar中读取属性文件,依此类推.

Gun*_*nar 5

不可能有多个ValidationMessages.properties文件.但您可以使用Hibernate Validators AggregateResourceBundleLocator从具有不同名称的bundle中检索消息,如下所示:

ValidatorFactory validatorFactory = configuration
    .messageInterpolator(
        new ResourceBundleMessageInterpolator(
            new AggregateResourceBundleLocator(
                Arrays.asList("foo", "bar"), 
                configuration.getDefaultResourceBundleLocator())))
    .buildDefaultValidatorFactory();
Run Code Online (Sandbox Code Playgroud)