繁体   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