![](/img/trans.png)
[英]In Spring Boot, how to pass a controller model variable to Thymeleaf th:src?
[英]In Spring Boot Thymeleaf , how to pass list of object to th:value ??? I would like to create tag input
<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>
@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.