Thymeleaf和SpringMVC的复选框

时间:2018-08-24 09:31:59

标签: spring checkbox thymeleaf

我正在尝试创建一个多复选框,但是它不起作用,错误在哪里? 这是我的代码:

checkorganization.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
 <form action="#" th:action="@{/checkorg}" th:object="${organization}" 
method="post">
<ul>
<li th:each="org : ${organizations}">
<input type="checkbox" th:field="*{organizations}" th:value="${org}" />
</li>
</ul>
</form>
</body>
</html>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

OrganizationController.java

@Controller
public class OrganizationController {
@Autowired
private OrganizationRepository organizationRepository;
@GetMapping("/checkorg")
public String checkOrg (Model model) {
       List<Organization> organizations = new ArrayList<Organization>();
       organizations = organizationRepository.findAll();
       Iterator<Organization> iterator = organizations.iterator();
       while (iterator.hasNext()) {
           model.addAttribute("organizations", iterator.next()); 
       }    
       return "checkorganization";
}

}

1 个答案:

答案 0 :(得分:0)

您必须对属性th:field和th:value使用数组或列表,例如String或integer的数组,而不是对象列表。 另外请注意,属性th:field是指所选复选框的列表。