How to populate a simple table with thymeleaf in spring mvc
28,964
Solution 1
You are missing your <TD>
tags providing the template which fields to print where. See the Iteration Basics of the documentation
Solution 2
Add <td>
tags under <th>
and then use the <td th:text="${persons.ID}"></td>
respectively to display the relevant data in the table.
Author by
stackUser2000
Updated on July 09, 2022Comments
-
stackUser2000 almost 2 years
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 }