Quick Start Guide: Using Thymeleaf with Spring Boot
This tutorial walks through the fundamentals of Thymeleaf, its key features, a side‑by‑side comparison with FreeMarker, and provides a complete Spring Boot example—including Maven dependencies, entity and controller code, template markup, and essential configuration settings—to help developers integrate the template engine quickly and correctly.
What Is Thymeleaf?
Thymeleaf is a Java library that can process HTML/HTML5, XML, JavaScript, CSS, and even plain text. It is typically used as the view layer in an MVC architecture and can fully replace JSP.
Key Features
Supports natural templating: extra attributes added to HTML tags are ignored by browsers, allowing the same file to be opened statically.
Out‑of‑the‑box support for standard and Spring dialects, enabling JSTL‑like expressions and custom extensions.
Provides Spring‑specific dialects for seamless form binding, property editors, internationalization, and more.
Compared with FreeMarker, Thymeleaf preserves the original document structure. For example:
<p th:text="${message}">Hello World!</p>Note: Because Thymeleaf uses an XML DOM parser, it is not suited for processing very large XML files.
Step‑by‑Step Example
Create a Spring Boot project and select the Thymeleaf starter during initialization.
Add the following dependencies to pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>Define a simple entity:
@Data
public class Student {
private String idNo;
private String name;
}Create a controller that returns a string and a list of Student objects:
@Controller
public class StudentController {
@GetMapping("/")
public String getStudents(Model model) {
List<Student> list = new ArrayList<>();
Student s1 = new Student();
s1.setIdNo("N01");
s1.setName("Tom");
list.add(s1);
Student s2 = new Student();
s2.setIdNo("N02");
s2.setName("David");
list.add(s2);
model.addAttribute("students", list);
model.addAttribute("hello", "Hello Thymeleaf!");
return "student";
}
}The corresponding student.html template (placed under src/main/resources/templates) looks like:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Hello Thymeleaf</title>
</head>
<body>
<h1 th:text="${hello}">Hello World</h1>
<div th:each="student : ${students}">
<p th:text="${student.name} + ':' + ${student.idNo}"></p>
</div>
</body>
</html>When you run the application and visit http://localhost:8080/, the page displays the greeting and the list of students.
Important Tips
During development, disable Thymeleaf’s template cache to see changes without restarting: spring.thymeleaf.cache=false In production, keep the default cache setting ( true) for better performance.
All Thymeleaf pages must declare the Thymeleaf namespace in the html tag:
<html xmlns:th="http://www.thymeleaf.org">Spring Boot Thymeleaf Configuration Properties
Spring Boot exposes many properties to fine‑tune Thymeleaf behavior:
# Enable template cache (default: true)
spring.thymeleaf.cache=true
# Check if template exists
spring.thymeleaf.check-template=true
# Verify template location (default: true)
spring.thymeleaf.check-template-location=true
# Content type (default: text/html)
spring.thymeleaf.content-type=text/html
# Enable MVC view resolver (default: true)
spring.thymeleaf.enabled=true
# Template encoding
spring.thymeleaf.encoding=UTF-8
# Prefix for view names (default: classpath:/templates/)
spring.thymeleaf.prefix=classpath:/templates/
# Suffix for view names (default: .html)
spring.thymeleaf.suffix=.html
# Template mode (HTML5 validates strictly)
spring.thymeleaf.mode=HTML5With these settings, integrating Thymeleaf into a Spring Boot project is straightforward.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
