亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將對象或數組從 html 頁面發送到 POST 方法中

如何將對象或數組從 html 頁面發送到 POST 方法中

江戶川亂折騰 2021-08-25 18:22:34
我有一個帶有下一個代碼的 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。)


查看完整回答
反對 回復 2021-08-25
  • 1 回答
  • 0 關注
  • 258 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號