📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Learn Thymeleaf at https://www.javaguides.net/p/thymeleaf-tutorial.html
<tr th:each="employee : ${employees}">
<td th:text="${employeeStat.index}"></td>
<td th:text="${employee.firstName}"></td>
<td th:text="${employee.lastName}"></td>
<td th:text="${employee.email}"></td>
</tr>
Complete Example - Thymeleaf Loop or Iteration Over a List by Index Example with Spring Boot
Maven Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Employee Model Class
package net.javaguides.springboot;
public class Employee {
private String firstName;
private String lastName;
private String email;
public Employee(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Spring MVC Controller - EmployeeController.java
package net.javaguides.springboot;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class EmployeeController {
@GetMapping("/iteration")
public String iteration(Model model) {
List < Employee > employees = new ArrayList < > ();
employees.add(new Employee("Ramesh", "Fadatare", "ramesh@gmail.com"));
employees.add(new Employee("John", "Cena", "john@gmail.com"));
employees.add(new Employee("Tom", "Cruise", "tom@gmail.com"));
employees.add(new Employee("Tony", "Stark", "tony@gmail.com"));
model.addAttribute("employees", employees);
return "iteration";
}
}
Thymeleaf template - iteration.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Add Bootstrap CSS Demo</title>
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row">
<h1>Employees</h1>
<table class="table">
<thead>
<tr>
<th>Index</th>
<th>Employee First Name</th>
<th>Employee Last Name</th>
<th>Employee Email</th>
</tr>
</thead>
<tbody>
<tr th:each="employee : ${employees}">
<td th:text="${employeeStat.index}"></td>
<td th:text="${employee.firstName}"></td>
<td th:text="${employee.lastName}"></td>
<td th:text="${employee.email}"></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Demo
Related Thymeleaf Tutorials and Examples
- Introducing Thymeleaf | Thymeleaf Template | Thymeleaf Template Engine
- Thymeleaf Example with Spring Boot
- How to Add CSS and JS to Thymeleaf
- Add Bootstrap CSS to Thymeleaf
- How to handle null values in Thymeleaf?
- How to Loop a List by Index in Thymeleaf
- Thymeleaf Array Example - Array Index, Array Iteration
- Thymeleaf Enums Example
- Thymeleaf If Else Condition Example
- Thymeleaf Switch Case Example
Comments
Post a Comment
Leave Comment