小编Tom*_*rma的帖子

Thymeleaf#lists.contains()表达式实用程序不起作用

我正在使用百里香标准方言并尝试在表单中呈现复选框列表.渲染是可以的,但问题是我尝试使用thymeleaf#lists.contains()表达式实用程序方法将"checked"属性应用于复选框.

所以我有一个包含以下字段的模型类:

private List<Template> templates;

@FormParam("selectedTemplates")
private List<String> selectedTemplates = Lists.newArrayList();
Run Code Online (Sandbox Code Playgroud)

一个Thymeleaf模板html片段:

<div th:each="template : *{templates}">
    <input type="checkbox" name="selectedTemplates" th:value="${template.id}" 
    th:checked="${#lists.contains(product.selectedTemplates, template.id)}" />
    <label th:text="${template.filename} + ' (' + ${template.description} + ')'" />
    <!-- Attempt to use the list contains to check the field -->
    <div th:text="${product.selectedTemplates}"/>
    <div th:text="${template.id}"/>  
    <div th:text="${#lists.contains(product.selectedTemplates, template.id)}" />
</div>
Run Code Online (Sandbox Code Playgroud)

页面上的输出应该选择一个复选框.

<input type="checkbox" name="selectedTemplates" value="4" /> (Template Name)
<div>[4,5]</div>
<div>4</div>
<div>false<div>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我打印了具有值[4,5]的列表,并使用#lists.contains方法查看它是否包含template.id,但是,该方法始终返回false.我甚至尝试了一些硬编码的id来测试方法,我总是得到"假"回来.

例如:

<div th:text="${product.selectedTemplates}"/>
<div th:text="${#lists.contains(product.selectedTemplates, 4)}" />
Run Code Online (Sandbox Code Playgroud)

打印[4,5]错误

<div th:text="${product.selectedTemplates}"/>
<div th:text="${#lists.contains(product.selectedTemplates, '4')}" /> …
Run Code Online (Sandbox Code Playgroud)

java expression thymeleaf

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

expression ×1

java ×1

thymeleaf ×1