zoukankan      html  css  js  c++  java
  • Hibernate-Validator 5.1.0.Final 无法解析自定义占位符的问题

    最近在做一个小项目,想使用Hibernate-validator来做后台数据的验证,在pom中:

    <!-- hibernate -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>${hibernate.validator.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>${hibernate.version}</version>
            </dependency>

    结果尝试了N多边都是失败的,在网上搜了很多相关资料,发现使用这个最新版本的jar文章微乎其微。今早,跟踪源代码进去找原因,发现每次在ResourceBundleMessageInterpolator.java类中的interpolateExpression方法[341行InterpolationTerm expression = new InterpolationTerm( term, locale );]中抛出异常——javax.el.ExpressionFactory无法实例化,继续跟踪代码进去,发现InterpolationTerm类的构造方法很简单

    public InterpolationTerm(String expression, Locale locale) {
            this.locale = locale;
            this.expression = expression;
            if ( expression.startsWith( EL_DESIGNATION_CHARACTER ) ) {
                this.type = InterpolationTermType.EL;
            }
            else {
                this.type = InterpolationTermType.PARAMETER;
            }
        }

    开始认为这里不可能抛出异常,又尝试了很多边,发现问题就出现InterpolationTerm实例化过程中,既然构造方法没问题,那么肯定在其他的static块或者方法中存在问题,继续找相关代码。编译器已经提示存在问题了

    static {
            expressionFactory = ExpressionFactory.newInstance();
        }

    问题在这里,ExpressionFactory工厂类里未定义newInstance方法。

    至此,问题原因找到了,是由于ExpressionFactory工厂类出错了。

    既然找到了错误,那么这个类是在哪个jar包里呢?

    结果发现这个类是在jsp-api.jar包里,那么也就是说hibernate-validator.5.1.0.Final不能使用jsp-api.jar的ExpressionFacotry类,那使用哪个呢?

    找到了原因,距离问题解决就不远了,继续在网上找答案……

    ExpressionFactory这个类是属于el-api.jar,解决办法就是引入下面的jar文件(会把el-api.jar导入)

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>el-impl</artifactId>
        <version>2.2</version>
    </dependency>

    至此问题解决了。

    备注:Tomcat6.x下问题依然存在,主要原因是servlet/jsp的api版本低;换成tomcat7.x及以上就没问题了

  • 相关阅读:
    [Daily Coding Problem 223] O(1) space in order traversal of a binary tree
    [Daily Coding Problem 224] Find smallest positive integer that is not the sum of a subset of a sorted array
    Sliding Window Algorithm Questions
    Sweep Line Algorithm Summary
    Palindrome Algorithm Questions
    Core Data Structures, Algorithms and Concepts
    [LeetCode] 1000. Minimum Cost to Merge Stones
    UVA 253 Cube painting(枚举 模拟)
    Codeforces 821C Okabe and Boxes
    Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
  • 原文地址:https://www.cnblogs.com/linxyz/p/3672937.html
Copyright © 2011-2022 走看看