3

I am using JSR 303 Bean validation in my JSF 2.0 web application and it works fine with annotations. Now I would like to ignore annotations and configure validation rules using the validation.xml file, so this is what I did (I am using an eclipse dynamic web project) :

  1. Added validation.xml under WebContent/META-INF/validation.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <validation-config
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd"
    >
    
      <constraint-mapping>META-INF/validation/constraint-mapping.xml</constraint-mapping>
    
    </validation-config>
    
  2. Then created the file constraint-mapping.xml under WebContent/META-INF/validation/constraint-mapping.xml

    <constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
                 xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
    
    <bean class="my.full.path.ValidationMB" ignore-annotations="true">
    
    </bean>
    
    </constraint-mappings>
    

Having these configurations in place, I suppose the annotations in my bean class ValidationMB shall be ignored, BUT this is not happening!, which makes me assume that the validation.xml file is not being loaded.

any ideas? thanks.

Environment:

  1. Apache Tomcat 7.0.23
  2. javax.faces-2.1.4.jar
  3. hibernate-validator-4.2.0.Final.jar
  4. hibernate-validator-annotation-processor-4.2.0.Final.jar
  5. validation-api-1.0.0.GA.jar
  6. slf4j-api-1.6.1.jar

From the spec: section 4.4.6. XML Configuration: META-INF/validation.xml

Unless explicitly ignored by calling Configuration.ignoreXMLConfiguration(), a Configuration takes into account the configuration available in META-INF/validation.xml. This configuration file is optional but can be used by applications to refine some of the Bean Validation behavior. If more than one META-INF/validation.xml file is found in the classpath, a ValidationException is raised.

Omar Al Kababji
  • 1,768
  • 4
  • 16
  • 31
  • I tried putting the validation.xml literally anywhere and it still doesn't work for me. Have you found out anything else than written here? – devsnd Mar 21 '12 at 17:11

3 Answers3

10

To solve my problem I had to create a META-INF folder under the project src folder, which ends in the WEB-INF/classes/META-INF.

The structure of the web application is:

ROOT
|_META-INF -- don't put validation.xml here
|_WEB-INF
    |__ classes
           |_META-INF
                |__validation.xml

But I think that if I pack my web application in a jar file and reuse it in another project It may not work, I will let you know later once I do it.

Omar Al Kababji
  • 1,768
  • 4
  • 16
  • 31
1

Try to put your validation.xml directly into the WEB-INF/ directory.

Ugo
  • 81
  • 5
  • 13
  • 3
    Tried that and it doesn't work, the spec of bean validation says META-INF/validation.xml – Omar Al Kababji Jan 02 '12 at 13:45
  • @OmarAlKababji Yes the specifications say that, but for me it did not work as well. I had to put it into my WEB-INF/ directory, but I can't really explane why. Try to remove your validation-api-1.0.0.GA.jar from the classpath, maybe you have conflicts in your application. – Ugo Jan 02 '12 at 14:52
  • That does not work with me either, what version of tomcat, jsf, are you using? where are you putting the mapping file? – Omar Al Kababji Jan 02 '12 at 17:35
  • 1
    I am working with JSF 2.0 on a WAS 8. All my configuration files are in WEB-INF, and so can be found in the classes/ folder since it's in the classpath. I guess your application were looking for a META-INF/validation.xml in your classpath, maybe because there's a configuration somewhere that specify that. – Ugo Jan 03 '12 at 09:19
  • having files in the WEB-INF directory does not mean that you get them in the WEB-INF/classes directory in runtime (at least in eclipse). In the java class org.hibernate.validator.xml.ValidationXmlParser there is a constant variable named: VALIDATION_XML_FILE = "META-INF/validation.xml"; so it seems for me that this file should be under META-INF somehow. – Omar Al Kababji Jan 03 '12 at 09:23
  • Yes, but I mean for instance if you have WEB-INF/ in your classpath, then all the file inside WEB-INF will be copied into the classes/ folder. So if you want your META-INF/ folder to be into your classes/ folder, maybe you should try to add the parent folder of META-INF/ into your classpath. But I think you will get issues by doing that. What if you put your META-INF/ directory into your WEB-INF/ directory ? – Ugo Jan 03 '12 at 13:34
1

I stumbled across this while looking for something else but wanted to clarify to the OP what is happening. You do in fact need the file to exist at META-INF/validation.xml; however, that is relative to the classpath which is why it worked when you put it under WEB-INF/classes/META-INF/validation.xml.

The cleaner approach is to let the file be put there for you. Your Eclipse project should already be outputting whatever is in your source directory to WEB-INF/classes somehow for you or nothing would be running. But sometimes there are filters on what it outputs so it might excluding something. You might want to check your src dirs and make sure they don't have exclusions.

Just as an example, if you had a Maven war project, all of your java sources would go in src/main/java and the generated classes would end up in the WEB-INF/classes directory. The equivalent happens for src/main/resources which contains non-source files. When I want *.xml, *.properties, etc. to end up in WEB-INF/classes I put them in src/main/resources. For your example I would have a src/main/resources/META-INF/validation.xml file.

Hope this helps anyone else who comes across this and is confused.

Andrew Wynham
  • 2,310
  • 21
  • 25