java 如何在 Thymeleaf 中为每个循环显示对象集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43266112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:15:43  来源:igfitidea点击:

How to display the collection of objects with for each loop in Thymeleaf?

javaspring-mvcthymeleaf

提问by Just4Fun

I want to display data from database in the browser with Spring MVC. Everything is alright, except my Thymeleaf template for each loop. Something is wrong there.

我想使用 Spring MVC 在浏览器中显示数据库中的数据。一切都很好,除了我的每个循环的 Thymeleaf 模板。那里有问题。

How can I display iddata in IDrow and namedata in Namerow iterating through the collection of objects with for eachloop?

如何id每个循环中遍历对象集合的ID行中显示数据和Name行中的name数据?

Source code:

源代码:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
     <title>Getting Started: Serving Web Content</title>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <table border="1">
        <tr style="font-size: 13">
            <td>ID</td>
            <td>Name</td>
        </tr>
        <tr th:each="count :  ${id}">
            <td><p th:text="${count}" /></td>       
            <td><p th:text="${name}" /></td>        

        </tr>
    </table>
</body>
</html>

回答by DimaSan

Your question is not very clear, as you didn't specify your countobject, and didn't show your controller.

你的问题不是很清楚,因为你没有指定你的count对象,也没有显示你的控制器。

Well supposing you have some entity Countwith fields idand name, which you persist in the corresponding table of your database, and which you want to display in the Thymeleaf template.

假设您有一些Count带有字段id和的实体name,您将其保存在数据库的相应表中,并且您希望将其显示在 Thymeleaf 模板中。

To retrieve data from the database you need some Serviceor Repositoryclass, that should have method that returns a Listof your entities, example of such service method listAll():

要从数据库中检索数据,您需要一些ServiceRepository类,该类应该具有返回List实体的方法,此类服务方法的示例listAll()

public List<Count> listAll() {
    List<Count> counts = new ArrayList<>();
    countRepository.findAll().forEach(counts::add);
    return counts;
}

Then you you need to set up the request mappingin your controller and add in that method an attribute to the modelobject, that will be a result of execution listAll()method. It may be done like:

然后,您需要在控制器中设置请求映射,并在该方法中向model对象添加一个属性,这将是执行listAll()方法的结果。可以这样做:

@RequestMapping("/list")
public String countsList(Model model) {
    model.addAttribute("counts", countService.listAll());
    return "list";
}

Finally answering your question, your list.htmltemplate should contain the block:

最后回答您的问题,您的list.html模板应包含以下块:

<div th:if="${not #lists.isEmpty(counts)}">
    <h2>Counts List</h2>
    <table class="table table-striped">
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        <tr th:each="count : ${counts}">
            <td th:text="${count.id}"></td>
            <td th:text="${count.name}"></td>
        </tr>
    </table>
</div>

Read more info in Thymeleaf Documentation - Iteration Basicssection.

在 Thymeleaf 文档 -迭代基础部分阅读更多信息。