Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 复选框属性已在Thymeleaf中选中_Spring Mvc_Thymeleaf - Fatal编程技术网

Spring mvc 复选框属性已在Thymeleaf中选中

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

如何使用Thymeleaf为表单中的输入设置checked属性? 这是我的代码,目前不起作用:

<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
属性需要一个布尔结果。我删除了第二个表达式。