在Spring Boot Thymeleaf中,如何将对象列表传递给th:value?我想创建标签输入。

huangapple go评论67阅读模式
英文:

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">添加新标签</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="顶部工具提示">添加</a>
    </form>
</div>
  1. 这是用于渲染表单的Get映射代码:
@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";
}
英文:
  1. Here is my form html
     &lt;div class=&quot;card-body&quot;&gt;
           &lt;form action=&quot;@{/orgCategoryTag/create}&quot;  method=&quot;POST&quot;&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                &lt;label for=&quot;form-tags-1&quot; class=&quot;text-dark font-bold&quot;&gt;Add new tags&lt;/label&gt;
                &lt;input id=&quot;form-tags-1&quot; name=&quot;tags-1&quot; type=&quot;text&quot; th:value=&quot;${tags}&quot;&gt;
              &lt;/div&gt;
              &lt;a
                  type=&quot;button&quot;
                  class=&quot;btn btn-success&quot; href=&quot;#&quot;
                  data-placement=&quot;top&quot; title=&quot;Tooltip on top&quot;&gt;Add
              &lt;/a&gt;
           &lt;/form&gt;
        &lt;/div&gt;
  1. Here is Get mapping for rendering form
    @GetMapping(value = &quot;/form&quot;)
        public String categoryForm(Model model, BindingResult result) {
            Long testId = (long)1;
            OrgCategory orgCategory = new OrgCategory();
            List&lt;OrgCategoryTagModel&gt; orgCategoryTags = orgCategoryTagRestController.getAllByCategoryId(testId);
            model.addAttribute(&quot;category&quot;, orgCategory);
            model.addAttribute(&quot;tags&quot;, orgCategoryTags);
            model.addAttribute(&quot;add&quot;, true);
            return &quot;orgCategoryForm&quot;;
        }

答案1

得分: 1

用于显示选项列表(在您的上下文中为标签)的,请使用组合框(combo-box),它们更适合用于显示选项列表。

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

<select> 是创建组合框的标签,而 <option> 则是组合框中的不同选项。我使用了一个 for each 循环来为组合框创建不同的选项,其中 tag 代表您的对象 OrgCategoryTagModel。

我不了解您的对象 OrgCategoryTagModel,但我假设您希望显示标签的名称(tag.name),并在进行选择时将 OrgCategoryTagModel 的 id(tag.id)用作要保存的值。

英文:

For displaying a list of options (tags in your context), please use a combo-box, they are better suited to display a list of options.

&lt;select&gt;
      &lt;option
           th:each=&quot;tag: ${tags}&quot;
           th:text=&quot;${tag.name}&quot;
           th:value=&quot;${tag.id}&quot;
      /&gt;
&lt;/select&gt;

&lt;select&gt; is the tag for creating a combobox and &lt;option&gt; are the different options available in the combobox. I have used a for each loop to create different options for the combobox where tag represents your object OrgCategoryTagModel.

I do not know your object OrgCategoryTagModel, but I am assuming you want to display the name of the tag (tag.name) and use the id of OrgCategoryTagModel (tag.id) as the value to be saved when making a selection.

huangapple
  • 本文由 发表于 2020年4月9日 17:24:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61117898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定