0

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>
2
  • I have 2 comments for you. First of all you shouldnt be providing any OGNL dependencies because it is already available from spring-boot-starter-thymeleaf . Secondly from my search i can only found this link blog.csdn.net/LX928525166/article/details/82699572 which is in Japanese but if you translate and follow the article you will see that with the help of a custom Access Member class it can be overcomed. Commented Mar 23, 2019 at 10:32
  • @Ahmet Thanks man. You were right about the OGNL dependency. I removed that. I was able to solve the problem. I was not aware that I need to provide getter functions for (public) members of the model class. No tutorial I saw mentioned that (or I overlooked it). Commented Mar 23, 2019 at 12:23

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.