I'm working with the thymeleaf standard dialect and trying to render a list of checkboxes in a form. The rendering is ok, however, the problem is where I try to apply the "checked" property to the checkboxes using the thymeleaf #lists.contains() expression utility method.
So I have a model class that has the following fields:
private List<Template> templates;
@FormParam("selectedTemplates")
private List<String> selectedTemplates = Lists.newArrayList();
A Thymeleaf template html fragment:
<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>
The output on the page for one of the checkboxes that should be selected.
<input type="checkbox" name="selectedTemplates" value="4" /> (Template Name)
<div>[4,5]</div>
<div>4</div>
<div>false<div>
So as you can see, I print the list which has values [4,5] and I use the #lists.contains method to see if it has template.id in it, however, the method always returns false. I even tried some hard coded ids to test the method and I always get "false" back.
For example:
<div th:text="${product.selectedTemplates}"/>
<div th:text="${#lists.contains(product.selectedTemplates, 4)}" />
Prints [4,5]false
<div th:text="${product.selectedTemplates}"/>
<div th:text="${#lists.contains(product.selectedTemplates, '4')}" />
Prints [4,5]false
Not sure what I'm doing wrong, but it seems so straight forward, not sure what else to try. My guess is there is something wrong with the syntax. Any suggestions or advice is greatly appreciated. I'm not able to find any resources on troubleshooting this problem, the thymeleaf guide quickly glosses over that section.