英文:
In Spring Boot Thymeleaf , how to pass list of object to th:value ??? I would like to create tag input
问题
- 这是我的表单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>
- 这是用于渲染表单的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";
}
英文:
- Here is my form 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>
- Here is Get mapping for rendering form
@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
得分: 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.
<select>
<option
th:each="tag: ${tags}"
th:text="${tag.name}"
th:value="${tag.id}"
/>
</select>
<select>
is the tag for creating a combobox and <option>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论