英文:
Spring Boot & Thymeleaf: list of links
问题
以下是翻译好的部分:
Java/Spring Boot web app here using Thymeleaf as the templating engine.
My bean:
public class InventoryItem {
private String modelNumber;
private String name;
// getters, setters and ctors omitted for brevity
}
My 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:
<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:
<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
. 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. Any ideas?
英文:
Java/Spring Boot web app here using Thymeleaf as the templating engine.
My bean:
public class InventoryItem {
private String modelNumber;
private String name;
// getters, setters and ctors omitted for brevity
}
My 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:
<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:
<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
. 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. Any ideas?
答案1
得分: 1
以下是翻译好的部分:
像这样可以工作...
<div class="col-md-4 mt-5">
<div class="panel-body">库存物品</div>
<ul>
<li th:each="item : ${inventory}">
<a th:href="@{/inventoryDetails/{modelNumber}(modelNumber=${item.modelNumber})}" th:text="${item.name}">鹅</a>
</li>
</ul>
</div>
如果您想要获取有关如何使用[链接URL][1]的更多信息,请参阅Thymeleaf文档。
[1]: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论