RuntimeException: MemberAccess implementation must be provided

99封情书 提交于 2019-12-02 15:03:28

问题


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>

However, as I am calling process() on the TemplateEngine:

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

I am getting this error:

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

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

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>

回答1:


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

In order to make it work.



来源:https://stackoverflow.com/questions/55306080/runtimeexception-memberaccess-implementation-must-be-provided

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!