如何将复选标记值列表提交到百里香叶的表单中



我正在尝试创建一个表,该表显示已添加的所有日志的列表。除了显示信息外,我还希望有另一列复选框,单击这些复选框时,我可以使用相应的删除按钮删除它们。

遇到的问题是我无法将复选框中的值放入 Long 数组中。我还想保持表格的功能正确显示。

对于我的表,我有以下代码:

<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>

我尝试将复选框值放入的形式是:(log.id 很长)

public class LogDeleteForm {
    private List<Long> logIds = new ArrayList<>();
    public List<Long> getLogIds() {
        return logIds;
    }
    public void setLogIds(List<Long> logIds) {
        this.logIds = logIds;
    }
}

在我的控制器中,我的视图有以下设置:

@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";
}

能够很好地实现删除,我只是无法获得我要删除的ID。如何将复选框值放入多头列表中?

原来我的问题出在我的 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";
}
我的重定向都是"重定向:/日志"

而不是"重定向:/项目/日志"

此外,我的按钮缺少 name="delete",因为它无法作为带有删除参数的提交。

最新更新