bean验证validation.xml被忽略 [英] bean-validation validation.xml ignored

查看:416
本文介绍了bean验证validation.xml被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的JSF 2.0 Web应用程序中使用JSR 303 Bean验证,它可以很好地与注释一起使用.现在,我想忽略注释并使用 validation.xml 文件配置验证规则,所以这就是我所做的(我正在使用Eclipse动态Web项目):

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

  • 然后在 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>
    

  • 有了这些配置后,我想我的bean类ValidationMB中的注释将被忽略,这没有发生!,这使我假设没有加载validation.xml文件.

    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.

    有什么想法吗?谢谢.

    环境:

    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

    根据规范:4.4.6节. XML配置:META-INF/validation.xml

    除非通过调用显式忽略 Configuration.ignoreXMLConfiguration(),Configuration会将其纳入 帐户META-INF/validation.xml中可用的配置.这 配置文件是可选的,但可以由应用程序用来配置文件. 优化一些Bean验证行为.如果不止一个 在类路径中找到META-INF/validation.xml文件, 引发ValidationException.

    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.

    推荐答案

    要解决我的问题,我必须在项目 src 文件夹下创建一个META-INF文件夹,该文件夹以WEB-INF结尾/classes/META-INF.

    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.

    Web应用程序的结构为:

    The structure of the web application is:

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

    但是我认为,如果将我的Web应用程序打包到jar文件中,然后在另一个项目中重用它,则可能无法正常工作,一旦完成,我将在稍后通知您.

    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.

    这篇关于bean验证validation.xml被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆