【问题标题】:How do I pass the selected dropdown value to a controller in Thymeleaf?如何将选定的下拉值传递给 Thymeleaf 中的控制器?
【发布时间】:2019-11-11 02:57:06
【问题描述】:

如何将下拉列表(Thymeleaf)的选中值传递给控制器​​(Spring)?列表本身形成正常,问题出在按钮上。

控制器:

@RequestMapping(value="courier/notInTime", method = RequestMethod.POST)
public String deleteUser (@RequestParam String task) {
    System.out.println(task);
    return "redirect:/courier";
}

查看:

<div class="taskList" th:object="${task}">
    <select class="form-control" id="courierTasks" name="courierTasks">
        <option value="">Select task for disable</option>
        <option th:each="task : ${tasks}"
                th:value="${task}"
                th:text="${task}">
        </option>
    </select>
    <form th:action="@{/courier/notInTime}" method="post">
        <input type="hidden"/>
        <button type="submit">Not in time</button>
    </form>
</div>

【问题讨论】:

    标签: spring spring-mvc thymeleaf


    【解决方案1】:

    您只需在控制器中将 @RequestParam 更改为 @Valid,然后在 Thymeleaf 中将您的选择名称更改为“任务”,它应该进入表单包装器。

    @RequestMapping(value="courier/notInTime", method = RequestMethod.POST)
    public String deleteUser (@Valid String task) {
        System.out.println(task);
        return "redirect:/courier";
    }
    
    <select class="form-control" id="courierTasks" name="task">
        <option value="">Select task for disable</option>
        <option th:each="task : ${tasks}"
                th:value="${task}"
                th:text="${task}">
        </option>
    </select>
    

    【讨论】:

    • 按钮呢?
    【解决方案2】:

    您应该在表单中添加选择标签,以便表单提交task

    试试下面的

    <form th:action="@{/courier/notInTime}" method="post">
      <div class="taskList" th:object="${task}">
        <select class="form-control" id="courierTasks" name="courierTasks">
            <option value="">Select task for disable</option>
            <option th:each="task : ${tasks}"
                    th:value="${task}"
                    th:text="${task}">
            </option>
        </select>
        <input type="hidden"/>
        <button type="submit">Not in time</button>
    
      </div>
    </form>
    

    请注意,您可以编写一个简单的 javascript 函数,以便表单提交task,即使它在表单之外,但是,我看不出在您的情况下不在表单中添加选择的原因。

    【讨论】:

      【解决方案3】:

      @Sopheak @Ioannis 非常感谢。

      控制器:

      @RequestMapping(value="courier/notInTime", method = RequestMethod.POST)
      public String deleteUser (@Valid String task) {
          System.out.println(task);
          return "redirect:/courier";
      }
      

      查看:

       <form th:action="@{/courier/notInTime}" method="post">
              <select class="form-control" id="task" name="task">
                  <option value="">Select task for disable</option>
                  <option th:each="task : ${tasks}"
                          th:value="${task}"
                          th:text="${task}">
                  </option>
              </select>
              <button type="submit">Not in time</button>
       </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-09
        • 2016-01-31
        • 1970-01-01
        • 1970-01-01
        • 2017-01-28
        相关资源
        最近更新 更多