Bean 验证@NotNull、@NotBlank 和@NotEmpty 在JSF+Tomcat 中不起作用

发布于 2024-12-06 12:26:58 字数 402 浏览 0 评论 0原文

我在 JSF 托管 bean 中使用 Hibernate Validation 注释。当我使用 @NotNull 、 @NotBlank 或 @NotEmpty 时,它们似乎不会以任何方式触发。

@NotBlank(message = "{name.required}")
public String name;

查看:

<h:outputLabel value="Name:" /> 
<h:inputText id="name" value="#{person.name}" size="20" />
<h:message for="name" style="color:red" />

这是怎么造成的,如何解决?

I am using Hibernate Validation annotations in my JSF managed bean. When I use @NotNull, @NotBlank or @NotEmpty they doesn't seem to be triggered in any way.

@NotBlank(message = "{name.required}")
public String name;

View:

<h:outputLabel value="Name:" /> 
<h:inputText id="name" value="#{person.name}" size="20" />
<h:message for="name" style="color:red" />

How is this caused and how can I solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赢得她心 2024-12-13 12:26:58

简介

由于您没有对我关于您正在使用什么容器的问题的评论提供任何反馈,因此我浏览了您的问题历史记录以了解您都在使用什么容器。到目前为止,我只发现 Tomcat 。因此,对于这个答案,我将假设您确实在使用 Tomcat,正如我在发布评论时最初猜测的那样。

确保安装所有 JAR

Tomcat 不附带任何开箱即用的 JSR303 Bean 验证 API/实现。您需要自己下载并安装它。您获得了要编译的注释意味着您已正确地将 hibernate-validator.jar 文件(命名可能因版本而异)放入 /WEB-INF/lib 文件夹中您的网络应用程序的。这些注释反过来似乎不起作用,只能意味着您没有阅读 readme.txt 和/或忘记从 /lib/required 添加 JAR Hibernate Validator 库 zip/tgz 文件的 文件夹:slf4j-api.jarvalidation-api.jar。为了让注释真正发挥作用,最后一项是强制性的。因此,要让 Hibernate Validator 在 Tomcat 中工作,您需要在 web 应用程序的 /WEB-INF/lib 中包含以下 JAR:

  • validation-api.jar (包含抽象 API 和注释扫描器)
  • hibernate-validator.jar (包含具体实现)
  • slf4j-api.jar (只是为了让它的记录器也能工作

)方式 @NotBlank@NotEmpty 必须工作。 @NotNull 值得特别关注;由于 HTTP 请求参数的性质,空输入字段默认以空字符串形式从客户端(Web 浏览器)接收。空字符串与 null 不同,因此 @NotNull 默认情况下永远不会启动。但是,JSF 可以配置为将它们解释为 null,只需将以下上下文参数添加到web.xml

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

这样 @NotNull 也必须有效。

BV 有效,但只有空字段无效

如果它仍然不起作用(即 3 个注释都不起作用,但其他注释如 @Size(min=5) 最小长度为 5 就可以了),那么机会很大您在 web.xml 中还有以下上下文参数:

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
</context-param>

然后您应该将其删除(默认为 auto,即仅当在运行时找到 JSR303 Bean Validation API 时) classpath)或将其设置为 true

BV 根本不起作用

当实际上 BV 不起作用时,也不起作用 @Size@Pattern 等,那么您应该验证您的表单中是否没有包含以下内容:

<f:validateBean disabled="true" />

然后您应该将其删除(它会默认启动)或设置 disabled="false"

确保您使用最新的 Mojarra

当 BV 仍然 不起作用时,请验证您是否使用的不是 2.2.3 到 2.2.6 之间的旧 Mojarra 版本。这些版本有一个类加载委托错误,导致 Tomcat 和克隆上的 Bean 验证完全不可见。这被报告为 Mojarra 问题 3183 并在 Mojarra 2.2.7 中修复。

Introduction

Since you didn't give any feedback on my comment with the question what container you're using, I peeked around in your question history to learn what containers you're all using. As far I found only Tomcat. So I'll for this answer assume that you're indeed using Tomcat as I initially guessed while posting the comment.

Make sure you install all JARs

Tomcat does not ship with any JSR303 Bean Validation API/implementation out the box. You need to download and install it yourself. That you got those annotations to compile means that you've correctly dropped the hibernate-validator.jar file (naming may differ per version) in /WEB-INF/lib folder of your webapp. That those annotations in turn does not seem to work in any way can only mean that you didn't read the readme.txt and/or forgot to add the JARs from the /lib/required folder of the Hibernate Validator library zip/tgz file: slf4j-api.jar and validation-api.jar. The last one is mandatory in order to get the annotations to actually work. So, to get Hibernate Validator to work in Tomcat, you need the following JARs in webapp's /WEB-INF/lib:

  • validation-api.jar (contains the abstract API and the annotation scanner)
  • hibernate-validator.jar (contains the concrete implementation)
  • slf4j-api.jar (just to get its logger to work as well)

This way @NotBlank and @NotEmpty must work. The @NotNull deserves special attention; empty input fields are namely by default received as empty strings from the client (webbrowser) due to the nature of HTTP request parameters. Empty strings are not the same as null, so the @NotNull will by default never kick in. JSF is however configureable to interpret them as null by just adding the following context parameter to web.xml:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

This way the @NotNull must work as well.

BV works but only empty fields not

If it still doesn't work (i.e. none of the 3 annotations work, but others like @Size(min=5) for a minimum length of 5 works fine), then chances are big that you've the following context parameter in web.xml as well:

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
</context-param>

You should then remove it (it defaults to auto, i.e. only when JSR303 Bean Validation API is found in runtime classpath) or to set it to true.

BV does not work at all

When actually nothing of BV works, also not @Size, @Pattern, etc, then you should verify if you do not have the following in your form:

<f:validateBean disabled="true" />

You should then remove it (it will just by default kick in) or to set disabled="false".

Make sure you use most recent Mojarra

When BV still doesn't work, then verify if you're not using an old Mojarra version between 2.2.3 and 2.2.6. Those versions had a classloading delegate bug causing Bean Validation on Tomcat and clones to be completely invisible. This is reported as Mojarra issue 3183 and fixed in Mojarra 2.2.7.

真心难拥有 2024-12-13 12:26:58

我有一个类似的问题,我能够通过在 web-inf lib 中包含以下三个 jar 来克服该问题。只需添加 zip 文件中提供的 hibernate 验证器 jar 和所需的 jar:

  • hibernate-validator-4.3.0.Final.jar
  • jboss-logging-3.1.0.CR2.jar< /code>
  • validation-api-1.0.0.GA.jar

I had a similar issue and i was able to overcome the problem by including the below three jars in web-inf lib. Just add the hibernate validator jar and the required jars as provided in the zip file:

  • hibernate-validator-4.3.0.Final.jar
  • jboss-logging-3.1.0.CR2.jar
  • validation-api-1.0.0.GA.jar
巾帼英雄 2024-12-13 12:26:58

添加

<context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>

在web.xml中

添加

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.23.Final</version>
        </dependency>
    

    <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-api</artifactId>
                <version>2.2.13</version>
            </dependency>
    
    
            <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-impl</artifactId>
                <version>2.2.13</version>
            </dependency> 

并在pom.xml中

add

<context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>

in web.xml

and add

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.23.Final</version>
        </dependency>
    

    <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-api</artifactId>
                <version>2.2.13</version>
            </dependency>
    
    
            <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-impl</artifactId>
                <version>2.2.13</version>
            </dependency> 

in pom.xml

芯好空 2024-12-13 12:26:58

如果即使添加了上述罐子后仍然无法验证,那么您可能错过了

 <mvc:annotation-driven />

在 spring 配置文件中添加标签。

If it still fails to validate even after adding the above indicated jars, then you might have missed adding tag

 <mvc:annotation-driven />

in the spring configuration file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文