簡體   English   中英

在 Spring 啟動 Thymeleaf 中,如何將 object 的列表傳遞給 th:value??? 我想創建標簽輸入

[英]In Spring Boot Thymeleaf , how to pass list of object to th:value ??? I would like to create tag input

  1. 這是我的表格 html
     <div class="card-body">
           <form action="@{/orgCategoryTag/create}"  method="POST">
              <div class="form-group">
                <label for="form-tags-1" class="text-dark font-bold">Add new tags</label>
                <input id="form-tags-1" name="tags-1" type="text" th:value="${tags}">
              </div>
              <a
                  type="button"
                  class="btn btn-success" href="#"
                  data-placement="top" title="Tooltip on top">Add
              </a>
           </form>
        </div>
  1. 這是獲取渲染表單的映射
    @GetMapping(value = "/form")
        public String categoryForm(Model model, BindingResult result) {
            Long testId = (long)1;
            OrgCategory orgCategory = new OrgCategory();
            List<OrgCategoryTagModel> orgCategoryTags = orgCategoryTagRestController.getAllByCategoryId(testId);
            model.addAttribute("category", orgCategory);
            model.addAttribute("tags", orgCategoryTags);
            model.addAttribute("add", true);
            return "orgCategoryForm";
        }

要顯示選項列表(上下文中的標簽),請使用組合框,它們更適合顯示選項列表。

<select>
      <option
           th:each="tag: ${tags}"
           th:text="${tag.name}"
           th:value="${tag.id}"
      />
</select>

<select>是用於創建 combobox 的標簽, <option>是 combobox 中可用的不同選項。 我使用 for each 循環為 combobox 創建不同的選項,其中標簽代表您的 object OrgCategoryTagModel。

我不知道您的 object OrgCategoryTagModel,但我假設您要顯示標簽的名稱 (tag.name) 並使用 OrgCategoryTagModel (tag.id) 的 id 作為進行選擇時要保存的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM