[英]Spring Boot & Thymeleaf: list of links
Java/Spring Boot web app here using Thymeleaf as the templating engine. Java/Spring Boot web 应用程序在这里使用 Thymeleaf 作为模板引擎。
My bean:我的豆子:
public class InventoryItem {
private String modelNumber;
private String name;
// getters, setters and ctors omitted for brevity
}
My Spring controller:我的 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";
}
}
And a snippet from the inventory.html
file that Thymeleaf must template:以及来自
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>
At runtime this produces a nice unordered list of inventory item names.在运行时,这会产生一个很好的库存项目名称的无序列表。
What I want now is to make this an unordered list of hyperlinks ( <a/>
) such that the rendered HTML looks like so:我现在想要的是使它成为一个无序列表的超链接(
<a/>
),使得呈现的 HTML 看起来像这样:
<ul>
<li><a href="/inventoryDetails/12345">Goose</a></li>
<li><a href="/inventoryDetails/23456">Duck</a></li>
<!-- etc. -->
</ul>
Where 12345
and 23456
are InventoryItem#modelNumbers
and where Goose
and Duck
are InventoryItem#names
.其中
12345
和23456
是InventoryItem#modelNumbers
,而Goose
和Duck
是InventoryItem#names
。 I've asked the Google Gods high and low and cannot find a working example of using Thymeleaf to render a list (ordered/unordered alike) of hyperlinks.我已经问过 Google Gods 高低,但找不到使用 Thymeleaf 呈现超链接列表(有序/无序)的工作示例。 Any ideas?
有任何想法吗?
Something like this would work...像这样的东西会起作用......
<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>
If you would like to get more information on how to work with Link URLs , follow Thymeleaf documentation.如果您想获得有关如何使用链接 URL的更多信息,请遵循 Thymeleaf 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.