Java 如何在 spring mvc 中用百里香叶填充一个简单的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25942358/
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-08-11 01:33:38  来源:igfitidea点击:

How to populate a simple table with thymeleaf in spring mvc

javahtmlspringspring-mvcthymeleaf

提问by stackUser2000

I have a list of objects and I want to display the values of those objects in a table using thymeleaf, this is what I have so far:

我有一个对象列表,我想使用 将这些对象的值显示在表中thymeleaf,这是我目前所拥有的:

Here is my controller class that adds my list of objects:

这是我的控制器类,它添加了我的对象列表:

@RequestMapping(value = "/showTableWithValues", method = RequestMethod.GET)
    public String showTableWithValues(Model model) 
    {
        //list with Persons
        ArrayList<Persons>  personsList= 
                new ArrayList<Persons>();

        personsList=  this.getListOfPersons();

        model.addAttribute("list", personsList);

        return "showTableWithValues";
    }

This is my .html file where i want to show my values:

这是我的 .html 文件,我想在其中显示我的值:

<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Home</title>
</head>
<body>
<h1>
    Show Values 
</h1>

<table class="table table-striped">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Cost</th>
                    <th>Mins to prepare</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>

                    <tr th:each="persons : ${list}">

                    </tr>   

                </tbody>
</table>
</body>
</html> 

And my Person.javaclass:

还有我的Person.java班级:

public class Person {

private String name;
private String last_name;
private String nickname;



.....get,setters and constructor
}

采纳答案by ndrone

You are missing your <TD>tags providing the template which fields to print where. See the Iteration Basics of the documentation

您缺少<TD>标签提供模板打印哪些字段。请参阅文档迭代基础

回答by Anthony Tumwes

Add <td>tags under <th>and then use the <td th:text="${persons.ID}"></td>respectively to display the relevant data in the table.

<td>在下面添加标签<th>,然后<td th:text="${persons.ID}"></td>分别使用来显示表格中的相关数据。