第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

是否可以使用 Thymeleaf 從列表中發(fā)布一個特定項目?

是否可以使用 Thymeleaf 從列表中發(fā)布一個特定項目?

三國紛爭 2021-08-19 13:33:49
在我的 Spring Boot 應(yīng)用程序中,我有以下內(nèi)容RequestMapping:@GetMapping("/test")public String get(Model model) {    List<CustomItem> items = itemService.findAll();    model.addAttribute("items", items);    return "test";}我將這些項目顯示在一個簡單的 HTML 表格中(一行表示一個項目)。我想向每一行添加一個按鈕,該按鈕僅提交與CustomItem端點相對應(yīng)的內(nèi)容,如下所示:@PostMapping("/test")public String post(CustomItem item) {    // doing something with item    return "redirect:/test";}我試過的是form為每一行創(chuàng)建一個單獨的:<table> <tr th:each="item, stat : ${items}">  <td>   <form th:object="${items[__${stat.index}__]}" th:action="@{/test}" method="post">    <input type="text" th:field="${items[__${stat.index}__].someField}">    <button type="submit">Submit</button>   </form>  </td> </tr></table>但是我在導(dǎo)航到頁面時收到以下錯誤:BindingResult 和 bean 名稱“items[0]”的普通目標(biāo)對象都不能用作請求屬性我也嘗試了以下方法:<table> <tr th:each="item, stat : ${items}">  <td>   <form th:object="${item}" th:action="@{/test}" method="post">    <input type="text" th:field="*{someField}">    <button type="submit">Submit</button>   </form>  </td> </tr></table>在這種情況下,錯誤如下:BindingResult 和 bean 名稱“item”的普通目標(biāo)對象都不能用作請求屬性我無法弄清楚我的方法有什么問題,所以我真的很感激任何建議。
查看完整描述

2 回答

?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗 獲得超9個贊

這對我有用:


在控制器中實例化 item 并設(shè)置為模型:


@GetMapping("/test")

public String get(Model model) {

    List<CustomItem> items = itemService.findAll();

    model.addAttribute("items", items);

    model.addAttribute("item", new CustomItem());

    return "test";

}

HTML:


<table>

        <tr th:each="i : ${items}">

            <form th:action="@{/test}" method="post" th:object="${item}">


                <td th:text="${i.id}" />

                <td th:text="${i.name}" />

            <td><input type="hidden" th:value="${i.id}" name="id" />

                <input type="hidden" th:value="${i.someField}" name="someField" />

                <button type="submit" name="action" value="remove">OK</button></td>

            </form>

        </tr>

    </table>

并在控制器中創(chuàng)建一個方法來處理該項目:


@PostMapping("/test")

    public String test(@ModelAttribute CustomItem item,HttpServletRequest request) {

        doStuff(item);

    }


查看完整回答
反對 回復(fù) 2021-08-19
?
米脂

TA貢獻(xiàn)1836條經(jīng)驗 獲得超3個贊

我通過簡單地使用th:value和name屬性而不是th:field:


<table>

 <tr th:each="item : ${items}">

  <td>

   <form th:action="@{/test}" method="post">

    <input type="text" th:value="${item.someField}" name="someField">

    <button type="submit">Submit</button>

   </form>

  </td>

 </tr>

</table>


查看完整回答
反對 回復(fù) 2021-08-19
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號