Spring mvc 复选框属性已在Thymeleaf中选中
如何使用Thymeleaf为表单中的输入设置checked属性? 这是我的代码,目前不起作用:Spring mvc 复选框属性已在Thymeleaf中选中,spring-mvc,thymeleaf,Spring Mvc,Thymeleaf,如何使用Thymeleaf为表单中的输入设置checked属性? 这是我的代码,目前不起作用: <label th:each="cat : ${categories}"> <input type="checkbox" value="" th:value="${cat.id}" th:text="${cat.description}" th:checked="${recipe.getCategories().contai
<label th:each="cat : ${categories}">
<input type="checkbox" value=""
th:value="${cat.id}"
th:text="${cat.description}"
th:checked="${recipe.getCategories().contains(cat) ? true : false}"
/>
</label>
如评论中所述,问题可能来自其他地方,但请尝试以下方法,看看是否有帮助:
th:checked="${recipe.getCategories().contains(cat)}"
同时添加相关代码,以便能够理解它返回的recipe.getCategories()…的确切内容,因为语法看起来不错。是的,问题出在其他地方。您使用的语法是有效的(虽然不需要三元组…
${recipe.getCategories().contains(cat)}
就足够了。getCategories()返回某个类CategoryCommand的哈希集,而categories是同一个类的哈希集,但包含()始终返回false。两个集合的所有对象都在数据库中。th:checked
属性需要一个布尔结果。我删除了第二个表达式。