【问题标题】:How to populate a simple table with thymeleaf in spring mvc如何在spring mvc中用百里香填充一个简单的表
【发布时间】:2014-11-14 13:07:14
【问题描述】:

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

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

@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";
    }

这是我的 .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> 

还有我的Person.java 班级:

public class Person {

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



.....get,setters and constructor
}

【问题讨论】:

  • 您忘记在 下添加 。建议你先刷html。

标签: java html spring spring-mvc thymeleaf


【解决方案1】:

您缺少提供模板的 &lt;TD&gt; 标记,这些标记提供了在哪里打印哪些字段。见Iteration Basics of the documentation

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-20
      • 2021-03-26
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2016-04-02
      • 2014-11-21
      • 2018-11-24
      相关资源
      最近更新 更多