Not getting JSR303 annotations to work with Tomcat 7

断了今生、忘了曾经 提交于 2019-12-02 19:32:55

问题


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.


回答1:


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.




回答2:


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 ....



来源:https://stackoverflow.com/questions/5731773/not-getting-jsr303-annotations-to-work-with-tomcat-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!