Thymeleaf表达式解析异常

时间:2018-07-27 17:49:50

标签: spring spring-boot thymeleaf

我有2个列表; allkontrols(2个元素),risk.kontrols(1个元素)

我将所有kontrols列为复选框,并尝试在risk.kontrols列表中包含它们的情况下“选择”它们。

Risk.kontrols中的一个元素也存在于allkontrols中。

因此,当我尝试打印此结果时:

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

我得到的结果是

true
false

到这里一切都还可以。现在,当我尝试用数据填充它们时,出现了错误。这是代码:

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

错误部分: 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)

1 个答案:

答案 0 :(得分:1)

看来您在{:th,selected中的{之前缺少一个$,尝试将其更改为:th:selected =“ $ {#lists.contains(risk.kontrols,kontrol)}”