Java 如何将复选标记值列表提交到Thymeleaf中的表单中?
我正在尝试创建一个表,其中显示已添加的所有日志的列表。除了显示信息外,我还希望有另一列复选框,单击这些复选框时,我可以使用相应的删除按钮删除它们 我遇到的问题是,我无法将复选框中的值放入long数组。我还希望保持表的功能正确显示 对于我的表格,我有以下代码:Java 如何将复选标记值列表提交到Thymeleaf中的表单中?,java,html,spring,spring-mvc,thymeleaf,Java,Html,Spring,Spring Mvc,Thymeleaf,我正在尝试创建一个表,其中显示已添加的所有日志的列表。除了显示信息外,我还希望有另一列复选框,单击这些复选框时,我可以使用相应的删除按钮删除它们 我遇到的问题是,我无法将复选框中的值放入long数组。我还希望保持表的功能正确显示 对于我的表格,我有以下代码: <form method="post" th:action="@{/projects/log/delete/}" th:object="${deleteForm}"> <div th:each="log : ${all
<form method="post" th:action="@{/projects/log/delete/}" th:object="${deleteForm}">
<div th:each="log : ${allLogs}" class="row">
<tbody>
<tr class="active">
<td>
<input type="checkbox" th:field="*{logIds}" th:value="${log.id}" />
</td>
<td th:text="${log.teamUsed}"></td>
<td th:text="${log.opponentStarters}"></td>
<td th:text="${log.opponentOthers}"></td>
<td th:text="${log.myStarters}"></td>
<td th:text="${log.myOthers}"></td>
<td th:text="${log.result}"></td>
</tr>
</tbody>
</div>
<button type="submit" id="deleteButton" class="hidden"></button>
</form>
我能够执行删除操作,但我无法获取我要删除的ID。如何才能将复选框值放入long列表中?我的问题在于deleteLogs方法:
@RequestMapping(value = "/log/delete", method = RequestMethod.POST, params = "delete")
public String deleteLogs(@ModelAttribute("deleteForm") logDeleteForm deleteForm) {
List<Long> formIds = deleteForm.getLogIds();
if (formIds == null || formIds.size() == 0) {
return "redirect:/projects/log";
}
for (Long id : formIds) {
logService.deleteLog(id);
}
return "redirect:/projects/log";
}
@RequestMapping(value=“/log/delete”,method=RequestMethod.POST,params=“delete”)
公共字符串deleteLogs(@modeldattribute(“deleteForm”)logDeleteForm deleteForm){
List formIds=deleteForm.getLogIds();
如果(formIds==null | | formIds.size()==0){
返回“重定向:/projects/log”;
}
for(长id:FormID){
logService.deleteLog(id);
}
返回“重定向:/projects/log”;
}
我的重定向都是“重定向:/log”,而不是“重定向:/projects/log”
此外,我的按钮缺少name=“delete”,因为它无法符合使用delete参数提交的条件
@RequestMapping(value = "pokemon_log", method = RequestMethod.GET)
public String view(Model model) {
model.addAttribute("addForm", new logForm());
model.addAttribute("deleteForm", new logDeleteForm());
model.addAttribute("allLogs", logService.getAllLogs());
return "log";
}
@RequestMapping(value = "/log/delete", method = RequestMethod.POST, params = "delete")
public String deleteLogs(@ModelAttribute("deleteForm") logDeleteForm deleteForm) {
List<Long> formIds = deleteForm.getLogIds();
if (formIds == null || formIds.size() == 0) {
return "redirect:/projects/log";
}
for (Long id : formIds) {
logService.deleteLog(id);
}
return "redirect:/projects/log";
}