Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read several ValidationMessages.properties from classpath

I have a problem with JSR303 and in special Hibernate-Validator.

I want to read a validation message from a property file. This property file lives in the war file that is deployed to the servlet container. The problem is, it's not accessed. I have the following project structure.

  • project-web (war file)
    • project-web-core (jar file in WEB-INF/lib of the project-web.war)
      • hibernate-validator jars (maven dependency of project-web-core)
      • ValidationMessages.properties (in src/main/resources of project-web-core.jar; this one is accessed)
    • ValidationMessages.properties (in src/main/resources of project-web.war; this one isn't accessed)

How can I access both ValidationMessages.properties? Or is this impossible?

Beyond that it would be perfect if the Hibernate-Validator first would read the property file in the war and if it wouldn't find the key, then read the property file from the jar and so on.

like image 205
lofthouses Avatar asked Jul 30 '12 10:07

lofthouses


1 Answers

It's not possible to have more than one ValidationMessages.properties file. But you could use Hibernate Validators AggregateResourceBundleLocator to retrieve messages from bundles with different names like this:

ValidatorFactory validatorFactory = configuration
    .messageInterpolator(
        new ResourceBundleMessageInterpolator(
            new AggregateResourceBundleLocator(
                Arrays.asList("foo", "bar"), 
                configuration.getDefaultResourceBundleLocator())))
    .buildDefaultValidatorFactory();
like image 181
Gunnar Avatar answered Nov 08 '22 20:11

Gunnar