📜 What is Thymeleaf?
✅ Thymeleaf is a modern server-side Java template engine
that is used to generate HTML, XML, JavaScript, CSS, or even plain text dynamically.
✅ It is mainly designed for web applications, but can also be used for email templates or any kind of dynamic content generation.
✅ Thymeleaf is often used as a replacement for JSP (JavaServer Pages) in modern Spring Boot and Spring MVC applications.
🔵 Key Characteristics of Thymeleaf
| Feature | Description |
|---|---|
| Template Engine | Processes HTML/XML templates on the server |
| Natural Templates | Templates can be opened directly in a browser even without a server (they look like normal HTML!) |
| Expression Language | Provides ${...}, th:* attributes to insert dynamic data |
| Spring Boot Integration | Very tightly integrated with Spring Boot (auto-configured) |
| Flexible Outputs | Can generate HTML5, XML, JavaScript, Text, etc. |
| Reusable Fragments | Supports templates and fragments (like header, footer reuse) |
| Conditionals and Loops | Easily write if/else, for each directly in HTML |
🔥 Small Example of Thymeleaf in a HTML Page
Suppose you have a Java controller that sets a model attribute:
model.addAttribute("username", "Stanley");
In your Thymeleaf template (welcome.html), you can use:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Welcome</title>
</head>
<body>
<h1 th:text="'Hello, ' + ${username} + '!'">Hello, Guest!</h1>
</body>
</html>
✅ The line:
<h1 th:text="'Hello, ' + ${username} + '!'">Hello, Guest!</h1>
Will replace the default content “Hello, Guest!” with “Hello, Stanley!” at runtime.
🔵 Some Important Thymeleaf Attributes
| Attribute | Purpose |
|---|---|
th:text | Set the inner text of an element |
th:href | Set the href attribute of a link |
th:src | Set the src attribute of an image |
th:if / th:unless | Conditional display of elements |
th:each | Looping over collections (like forEach) |
th:fragment | Define a reusable piece of HTML |
th:replace | Replace part of a page with a fragment |
🛠️ Example of a Loop with Thymeleaf
Suppose you have a list of products:
model.addAttribute("products", List.of("Apple", "Banana", "Cherry"));
In Thymeleaf:
<ul>
<li th:each="product : ${products}" th:text="${product}">Product</li>
</ul>
✅ Will generate:
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>
</ul>
🎯 Why is Thymeleaf Popular?
| Reason | Explanation |
|---|---|
| Natural Templates | You can open them directly as static HTML when designing. |
| Spring Support | Full integration with Spring MVC and Spring Boot. |
| Modern Syntax | Clean, HTML5-compliant, powerful expressions. |
| Reusable Components | Layouts, fragments, template inheritance. |
| Safe Evaluation | Graceful handling of nulls, missing values, etc. |
| Easy learning curve | Especially for those who already know HTML and basic Java. |
📢 In short:
✅ Thymeleaf is like JSP for modern Spring Boot applications — but smarter, cleaner, and much easier to work with.
✅ It uses pure HTML files with special attributes (th:*) to dynamically insert data.
🚀 Quick Real-World Usage
- Spring Boot auto-configures Thymeleaf.
- Place your
.htmlfiles inside/resources/templates/. - Thymeleaf renders pages dynamically with model data.
- You can build full web apps without touching JSP!