【问题标题】:How to submit a list of checkmark values into a form in Thymeleaf?如何将复选标记值列表提交到 Thymeleaf 中的表单中?
【发布时间】:2016-02-03 01:40:38
【问题描述】:

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

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

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

<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。如何将复选框值放入 long 列表中?

【问题讨论】:

    标签: java html spring spring-mvc thymeleaf


    【解决方案1】:

    原来我的问题出在我的 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";
    }
    

    我的重定向都是“redirect:/log”而不是“redirect:/projects/log”

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2011-02-26
      • 2015-09-03
      • 1970-01-01
      • 2019-10-09
      • 2014-04-04
      • 1970-01-01
      相关资源
      最近更新 更多