stackUser2000
stackUser2000

Reputation: 1695

How to populate a simple table with thymeleaf in spring mvc

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:

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 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.java class:

public class Person {

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



.....get,setters and constructor
}

Upvotes: 3

Views: 30820

Answers (2)

Tony Banks
Tony Banks

Reputation: 139

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

Upvotes: 2

ndrone
ndrone

Reputation: 3572

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

Upvotes: 8

Related Questions