1. Introduction
Thymeleaf is a Java-based templates engine to process HTML, XML, JS, CSS or Plain Text files. It comes with many utility objects and methods use to get useful information about variables, aggregate items in collections, joining them, format date and many more. In this article, we will present Thymeleaf utility methods for Lists. If you want to check the implementation of these methods go here.
2. Available utility methods for lists
Lists utilities are usually available under #lists
expression object. This object has several methods that can help us perform common tasks in our expressions.
Method | Purpose | Description |
---|---|---|
${#lists.toList(object)} |
Converts to list | Method converts Arrays or objects that implements Iterable interface or any kind of collection to List. |
${#lists.size(list)} |
Compute size | Use this method to get the size of a list. This method will automatically check if given parameter is null. |
${#lists.isEmpty(list)} |
Check whether list is empty | Function checks if list given in parameter is either null or empty. |
${#lists.contains(list, element)} |
Check if element is contained in list | Method checks if list contains one specified element given in the second parameter. |
${#lists.containsAll(list, elements)} |
Check if elements are contained in list | Method returns true if all elements are in the specified list. |
${#lists.sort(list)} |
Sort a copy of the given list | The members of the list must implement a comparable interface |
${#lists.sort(list, comparator)} |
Sort a copy of the given list with comparator | This sort method required that the comparator will be provided as a second parameter |
3. Usage example
We created an employee
model for this example application purpose with simple structure:
package com.frontbackend.thymeleaf.employee.model;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class Employee {
private String name;
private Double salary;
private EmployeePosition position;
}
Employee position is hold in enum
with the following available values:
package com.frontbackend.thymeleaf.employee.model;
public enum EmployeePosition {
MANAGER,
DIRECTOR,
CHIEF,
SUPERVISOR
}
Spring controller is used to handler GET requests on /employees
endpoint. We bind two parameters on result model
: a sample list of employees and a comparator used to compare employees by their salary.
package com.frontbackend.thymeleaf.employee.controller;
import com.frontbackend.thymeleaf.employee.model.Employee;
import com.frontbackend.thymeleaf.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Comparator;
@Controller
public class EmployeeController {
private final EmployeeService employeeService;
@Autowired
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@GetMapping("employees")
public String main(Model model) {
model.addAttribute("employees", employeeService.generateEmployeeList());
model.addAttribute("bySalary", Comparator.comparing(Employee::getSalary));
return "employees";
}
}
Spring Boot application server is used as a web container:
package com.frontbackend.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The sample Thymeleaf template has the following structure:
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring Boot Thymeleaf Application - Utility Lists</title>
</head>
<body>
<th:block th:if="${#lists.isEmpty(employees)}">List of employees is empty</th:block>
<h1>List of Employees (number of employees: <th:block th:text="${#lists.size(employees)}">100</th:block>)</h1>
<table>
<tr>
<th>No</th>
<th>Name</th>
<th>Salary</th>
<th>Position</th>
</tr>
<tr th:each="employee, stat : ${employees}">
<td th:text="${stat.index + 1}">1</td>
<td th:text="${employee.name}">John Doe</td>
<td th:text="${employee.salary}">12221</td>
<td th:text="${#strings.toLowerCase(employee.position.name())}">director</td>
</tr>
</table>
<h1>Sorted by salary</h1>
<table>
<tr>
<th>No</th>
<th>Name</th>
<th>Salary</th>
<th>Position</th>
</tr>
<tr th:each="employee, stat : ${#lists.sort(employees, bySalary)}">
<td th:text="${stat.index + 1}">1</td>
<td th:text="${employee.name}">John Doe</td>
<td th:text="${employee.salary}">12221</td>
<td th:text="${#strings.toLowerCase(employee.position.name())}">director</td>
</tr>
</table>
</body>
</html>
Results presented on http://localhost:8080/employees page looks like the following
4. Conclusion
In this article, we presented utility methods available in Thymeleaf for Lists. Utils are useful when we want to perform some specific tasks in our variable expressions. For example, if we want to sort collection before showing its items.
As usual the example used in this article can be found in our GitHub repo.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}