必须提供MemberAccess实现

必须提供MemberAccess实现

本文介绍了RuntimeException:必须提供MemberAccess实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a template which accesses public fields of a Customer object like this:

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

但是,当我在TemplateEngine上调用process()时:

However, as I am calling process() on the 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库中缺少某些内容,这就是为什么我添加了依赖项的原因:

I thought this might be because I am missing something from this ognl library which is why I added the dependency:

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

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

and tried different versions including the latest one. This however gives me:

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:

I can't find anything regarding this issue. I am using Spring Boot 2.1.1 and 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>

推荐答案

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

As @Ahmet stated in the comments, I do not need any additional OGNL dependencies. Would've surprised me anyway. The problem was, that my model class looked like this:

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

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

I was not aware, that there are explicit public getter methods required. I had to change the model to

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;
    }
}

为了使其正常工作.

这篇关于RuntimeException:必须提供MemberAccess实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:04