RuntimeException:必须提供MemberAccess实现

时间:2019-03-22 18:50:05

标签: spring spring-boot thymeleaf ognl

我正在使用一个模板来访问Customer对象的公共字段,如下所示:

<div>
    <div th:text="${customer.addressee}"></div>
    <div th:text="${customer.street}"></div>
    <div th:text="${customer.postalCode}, ${customer.city}"></div>
    <div th:text="${customer.country}"></div>
</div>

但是,当我在process()上致电TemplateEngine时:

templateEngine.process(String.format("invoice/%s.html", locale), context);

我收到此错误:

Caused by: java.lang.ClassNotFoundException: ognl.PropertyAccessor
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 40 more

我认为这可能是因为我在ognl库中缺少某些内容,这就是我添加依赖项的原因:

    <dependency>
        <groupId>ognl</groupId>
        <artifactId>ognl</artifactId>
        <version>3.2.10</version>
    </dependency>

并尝试了不同的版本,包括最新版本。但是,这给了我

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "customer.addressee" (template: "invoice/de.html" - line 42, col 10)

    at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:191)
    at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:95)
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)
    at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProces
    ...
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "customer.addressee"
    at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:191)
    at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:177)
    ... 48 more
Caused by: java.lang.RuntimeException: MemberAccess implementation must be provided!
    at ognl.OgnlContext.<init>(OgnlContext.java:140)
    at ognl.OgnlContext.<init>(OgnlContext.java:120)
    at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.executeExpression(OGNLVariableExpressionEvaluator.java:315)
    at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:170)
    ... 49 more

关于此问题,我找不到任何东西。我正在使用Spring Boot 2.1.1和ognl 3.2.10:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>ognl</groupId>
        <artifactId>ognl</artifactId>
        <version>3.2.10</version>
    </dependency>

</dependencies>

1 个答案:

答案 0 :(得分:0)

正如@Ahmet在评论中所述,我不需要任何其他OGNL依赖项。无论如何我都会感到惊讶。问题是,我的模型类看起来像这样:

public class Customer {
    public String addressee;
    public String street;
    public String postalCode;
    public String city;
    public String country;
}

我不知道,需要明确的公共获取方法。我不得不将模型更改为

public class Customer {
    private String addressee;
    private String street;
    private String postalCode;
    private String city;
    private String country;

    public String getAddressee() {
        return addressee;
    }

    public void setAddressee(String addressee) {
        this.addressee = addressee;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

为了使其正常工作。