Search code examples
springspring-bootthymeleaf

Thymeleaf expression parse exception


I've 2 lists; allkontrols (2 element), risk.kontrols (1 element)

I'm listing all kontrols as checkbox and trying to 'select' them if risk.kontrols list contains it.

The one element in risk.kontrols is also in allkontrols.

So, when I tried to print the results of this:

<div th:each="kontrol : ${allkontrols}" th:text="${#lists.contains(risk.kontrols, kontrol)}"/>

I got results as

true
false

Everything is ok till here. Now, when I tried to populate them with data, I got errors. Here is the code:

<div th:each="kontrol : ${allkontrols}" class="items form-control-lg">
    <label>
        <input th:value="${kontrol.id}" th:selected="{#lists.contains(risk.kontrols, kontrol)}" type="checkbox" class="flat">
        <div style="display: inline-block;" th:utext="  ${kontrol.name}"></div>
    </label>
</div>

Error part: th:selected="{#lists.contains(risk.kontrols, kontrol)}"

Stacktrace:

2018-07-27 20:36:55 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "view/riskkontrol.html")] with root cause
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "{#lists.contains(risk.kontrols, kontrol)}" (template: "riskkontrol" - line 44, col 140)

Solution

  • It looks like you're missing a $ before the { in your th:selected, try changing it to: th:selected="${#lists.contains(risk.kontrols, kontrol)}"