Thymeleaf is a Java server-side template engine that processes HTML, XML, and XHTML. For Spring Boot applications, it integrates cleanly through auto-configuration, making it a popular choice for building web views.
This guide covers integrating Thymeleaf with Spring Boot, passing data from controllers to views, and rendering lists and maps in your templates.
Adding Thymeleaf to Spring Boot
Spring Boot provides auto-configuration for Thymeleaf. Add the starter dependency to your project and you're ready—no manual configuration required.
The XML dependency includes the group ID, artifact ID, and version. For Maven projects, this goes in your pom.xml. Gradle users adapt the format accordingly.
Once added, Spring Boot automatically configures the template resolver, template engine, and view resolver. You can start writing templates immediately.
Enabling Thymeleaf Syntax in HTML
To use Thymeleaf attributes in your HTML files, add the Thymeleaf namespace to your HTML tag. This enables your editor to provide syntax highlighting and validation for Thymeleaf expressions.
With the namespace in place, Thymeleaf attributes like th:text and th:each become available for use.
Passing Data from Controller to View
Spring MVC controllers pass data to views through model attributes. Add attributes in your controller method and they're available in your template.
For simple data like strings or numbers, use model.addAttribute with a key and value. In your template, reference the attribute by name using ${attributename}.
The controller sets model.addAttribute("name", "John"), and the template displays it with th:text="$name". The output becomes the actual value rather than the placeholder.
Rendering Lists in Thymeleaf
Lists require iteration. Use th:each to loop through collections and display each item.
Define an Employee class with the fields you need. Populate a list in your controller and add it as a model attribute. In your template, use th:each="employee: ${employees}" to iterate. Within the loop, reference employee.id and employee.name for each item.
Thymeleaf extracts each object from the list in sequence, making the current item available within the loop body. This pattern works for displaying tables, lists, or any repeated content.
Working with Maps
Maps require slightly different handling because they contain key-value pairs. The th:each still iterates, but you access entries through entry.key and entry.value.
In your controller, populate a Map<Long, String> with employee data. Add it as a model attribute. In your template, iterate with th:each="entry: ${employeesMap}" and display entry.key and entry.value.
Our Analysis
Thymeleaf's Spring Boot integration removes most configuration overhead. The auto-configuration handles template resolution and rendering, letting you focus on building features.
The attribute-based syntax keeps templates readable. Unlike some templating engines that embed logic in HTML, Thymeleaf's th: attributes are explicit about what's dynamic versus static.
For developers building Spring MVC applications, Thymeleaf offers a clean separation between backend logic and frontend presentation. The natural HTML syntax makes templates easier to maintain and test.
What to watch next: Thymeleaf supports fragments, layouts, and conditional rendering that enable sophisticated template composition. Explore these features as your views grow more complex.
Key takeaways
- Spring Boot auto-configures Thymeleaf with the starter dependency
- Use model attributes to pass data from controllers to views
- Th:each iterates over lists and maps
- Thymeleaf attributes enable dynamic content without leaving HTML syntax
Frequently asked questions
How do I add Thymeleaf to a Spring Boot project?
Add the spring-boot-starter-thymeleaf dependency. Spring Boot handles configuration automatically.
How do I pass data from a controller to a Thymeleaf view?
Use model.addAttribute("key", value) in your controller. Reference it in the template with ${key}.
How do I iterate over a list in Thymeleaf?
Use th:each="item: ${listName}" where item is the variable name and listName is your model attribute.
What's the difference between rendering lists and maps?
Lists iterate over objects directly. Maps iterate over entries, accessing both key and value through entry.key and entry.value.