简体   繁体   中英

Not getting JSR303 annotations to work with Tomcat 7

After a couple hours of Google and a couple of tutorials I'm beat... It's not the fact that I'm getting errors that can give me hints on what the problem is, it's the complete lack of them that's driving me mad!

The following code works, just not as it should! The annotations for checking that the input is not null or lesser then 3 character long just never runs. Nor are they giving any kind of error when the project is being deployed or when the name variable is being written to.

public class testBean
{
    @NotNull
    @Size(min=3)
    private String name;
}

public void test()
{
    System.out.println(name);
}

And the input form:

<h:form>
        <h:inputText value="#{testBean.name}" />
    <h:commandButton value="Send" action="#{testBean.test()}" />
</h:form>

My guess is that it's tomcat that's causing the problems, although I am fairly sure I've imported all the necessary libraries. Same goes for the IDE I'm using, Eclipse.

As for the libs, I've inluded the "validation-api-1.0.0.GA.jar" both in the "WEB-INF/lib" folder and also the "apache-tomcat-7/lib" folder.

You've installed only the API, not the implementation. The API is an abstract contract so that you can declare/use it in your code and have the freedom in choosing the implementation. The implementation contains the concrete code and does the real work. You need to install the impl as well. The JSR-303 reference implemtation is the Hibernate Validator . The download details are here . Currently the latest final is 4.1.0 . It contains the hibernate-validator-4.1.0.Final.jar file which is the real implementation. Drop it in Webapp/WEB-INF/lib . It does not necessarily need to be in Tomcat7/lib , I'd remove the API from there as well to prevent future classpath collisions.

I had the same kind of problem, and search for a while, in my case, using hibernate-validator-4.2.0.Final.jar, I was lacking slf4j-api-1.6.4.jar. The problem is that hibernate validator does not crash but does NOT tell it is not working ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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