我有一個帶有下一個代碼的 html 頁面:<div class="w3-container w3-light-grey"> <div class="w3-container w3-grey"> <form th:action="@{'/wordprocess/' + ${descriptionId}}" method="post"> <table class="w3-table-all w3-card-4"> <tr> <th>Id</th> <th>Word</th> <th>Type</th> <th></th> </tr> <tr th:each="wd : ${wordslist}"> <td th:text="${wd.getId()}">Jill</td> <td th:text="${wd.getWord()}">Jill</td> <td th:if="${wd.getType() == 0}" th:text="word">Jill</td> <td th:if="${wd.getType() == 1}" th:text="skill">Jill</td> <td> <p> <input class="w3-check" type="checkbox" name="type" th:value="${wd.getType()}"> <label>Skill</label> </p> </td> </tr> </table> <input class="w3-button w3-black" type="submit" value="Save"/> </form> </div></div>在 \p\ 塊中,我使用復選框來初始化對象類型。字段 'type' 可以獲得兩個整數值:0 或 1。我希望能夠在復選框中指定這種類型的值(1 - 已選中,0 - 未選中)并在 POST 方法中獲取此數組:@RequestMapping(value = "/wordprocess/{descriptionId}", method = RequestMethod.POST)public String save( @PathVariable long descriptionId, @RequestParam(value = "type", required = false) int[] type){ System.out.println(descriptionId); System.out.println(type[0]); return "index";}但是在 save() 方法中,我在 'type' 變量中有一個空值。如何在 POST 方法中正確發送一組復選框值?
1 回答

天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
第一條規則,使用復選框/單選按鈕,您不會將未選中的項目返回到服務器,而只會將選中的項目返回給服務器。如果您將 HttpServletRequest 請求添加到 save() 方法的參數列表(它將由 Spring 自動填充),您應該能夠調用request.getParameterValues("type")
名稱為“type”的所有復選框的值屬性。您需要將所有這些復選框的值屬性更新為 wd.getId() 而不是 wd.getType() 以便您擁有選中項目的 ID。
最后,要指定初始選中/未選中狀態,請使用th:checked="${wd.getType()}"
th:value 代替(后者將需要保存如上所述的 id。)
添加回答
舉報
0/150
提交
取消