[英]Spring Boot & Thymeleaf: list of links
Java/Spring Boot web 应用程序在这里使用 Thymeleaf 作为模板引擎。
我的豆子:
public class InventoryItem {
private String modelNumber;
private String name;
// getters, setters and ctors omitted for brevity
}
我的 Spring controller:
@Controller
@RequestMapping("/inventory")
public class InventoryController {
@GetMapping("/{inventoryId}")
public String viewInventory(@PathVariable("inventoryId") String inventoryId, Model model) {
List<InventoryItem> inventory = getSomehow(inventoryId);
model.addAttribute("inventory", inventory);
return "inventory";
}
}
以及来自inventory.html
文件的片段,Thymeleaf必须模板:
<div class="col-md-4 mt-5">
<div class="panel-body">Inventory Items</div>
<ul>
<li th:each="item :${inventory}" th:text="${item.name}"></li>
</ul>
</div>
在运行时,这会产生一个很好的库存项目名称的无序列表。
我现在想要的是使它成为一个无序列表的超链接( <a/>
),使得呈现的 HTML 看起来像这样:
<ul>
<li><a href="/inventoryDetails/12345">Goose</a></li>
<li><a href="/inventoryDetails/23456">Duck</a></li>
<!-- etc. -->
</ul>
其中12345
和23456
是InventoryItem#modelNumbers
,而Goose
和Duck
是InventoryItem#names
。 我已经问过 Google Gods 高低,但找不到使用 Thymeleaf 呈现超链接列表(有序/无序)的工作示例。 有任何想法吗?
像这样的东西会起作用......
<div class="col-md-4 mt-5">
<div class="panel-body">Inventory Items</div>
<ul>
<li th:each="item :${inventory}">
<a th:href="@{/inventoryDetails/{modelNumber}(modelNumber=${item.modelNumber})}" th:text="${item.name}">Goose</a>
</li>
</ul>
</div>
如果您想获得有关如何使用链接 URL的更多信息,请遵循 Thymeleaf 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.