Thymeleaf Tutorial
Thymeleaf is a modern server-side Java template engine used for web and standalone environments. Thymeleaf is particularly useful in Spring Boot applications, allowing for easy and dynamic generation of HTML pages. This tutorial will cover the basics of using Thymeleaf with Spring Boot.
1. Setting Up Thymeleaf in Spring Bootβ
Thymeleaf is the default templating engine for Spring Boot. Start by creating a Spring Boot project with Thymeleaf dependency.
Adding Thymeleaf Dependencyβ
Add Thymeleaf dependency in your pom.xml (for Maven) or build.gradle (for Gradle):
Mavenβ
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. Basic Thymeleaf Syntaxβ
Thymeleaf uses a special th: attribute to define dynamic data.
Example HTML Templateβ
In src/main/resources/templates/index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf Example</title>
</head>
<body>
<h1 th:text="'Hello, ' + ${name}">Hello, User!</h1>
</body>
</html>
In this example, th:text replaces the content of <h1> with the variable name.
3. Thymeleaf Expressionsβ
Thymeleaf expressions allow dynamic content in templates.
- Variable Expressions:
${...}to display variables. - Text Replacement:
th:textreplaces the text content.
Exampleβ
<p th:text="${welcomeMessage}">Welcome message here</p>
4. Iteration with Thymeleafβ
Use th:each to iterate over lists.
Exampleβ
<ul>
<li th:each="item : ${items}" th:text="${item}">Item</li>
</ul>
If items is a list of strings, this will generate a list of <li> elements.
5. Conditional Renderingβ
Use th:if and th:unless for conditional rendering.
Exampleβ
<p th:if="${isMember}">Welcome back, member!</p>
<p th:unless="${isMember}">Please sign up to continue.</p>
6. Form Handlingβ
Thymeleaf integrates well with Springβs form handling.
Example Formβ
<form action="#" th:action="@{/submit}" th:object="${user}" method="post">
<label for="name">Name:</label>
<input type="text" id="name" th:field="*{name}">
<button type="submit">Submit</button>
</form>
In your controller, bind user to a model attribute.
7. Using Fragmentsβ
Thymeleaf fragments allow you to reuse parts of a template.
Creating a Fragmentβ
In src/main/resources/templates/fragments.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="header">
<h1>Header Section</h1>
</div>
</body>
</html>
Including a Fragmentβ
In your main template:
<div th:include="fragments :: header"></div>
8. URL Expressionsβ
Use @{...} for URL expressions, useful for linking resources and forms.
Exampleβ
<a th:href="@{/home}">Home</a>
9. Built-in Thymeleaf Utility Objectsβ
Thymeleaf provides built-in objects like:
#dates: For date formatting.#numbers: For number formatting.#strings: For string utilities.
Exampleβ
<p th:text="${#dates.format(myDate, 'yyyy-MM-dd')}">Formatted Date</p>
Summaryβ
This tutorial covered the basics of using Thymeleaf with Spring Boot:
- Setting up Thymeleaf in a Spring Boot project.
- Using Thymeleaf attributes like
th:text,th:each, andth:if. - Handling forms and fragment reuse.
- Utilizing expressions for URLs and variables.
Thymeleaf is a powerful templating engine that simplifies HTML generation in Java-based web applications.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.