How do you create a Spring Boot application that uses Thymeleaf for rendering views?
Table of Contents
- Introduction
- Setting Up a Spring Boot Application with Thymeleaf
- Practical Example: Dynamic Data in Thymeleaf
- Conclusion
Introduction
Thymeleaf is a popular templating engine used in Java-based web applications for rendering dynamic views. It integrates seamlessly with Spring Boot to create powerful and scalable web applications. Thymeleaf allows you to define HTML templates with embedded dynamic content using its rich syntax and expressions. In this guide, we’ll walk through the process of creating a Spring Boot application that uses Thymeleaf for rendering views.
Setting Up a Spring Boot Application with Thymeleaf
1. Create a Spring Boot Application
First, you’ll need to create a Spring Boot project. You can either use Spring Initializr (https://start.spring.io/) or your IDE to generate the project. For this example, we’ll use Spring Initializr.
- Dependencies: Choose the following dependencies:
- Spring Web: For creating web applications.
- Thymeleaf: For integrating Thymeleaf as the templating engine.
Download the generated ZIP file and extract it, or directly open it in your IDE.
2. Add Dependencies in pom.xml
If you’re creating the project manually or using an existing Spring Boot project, make sure the following dependencies are included in the pom.xml
file:
The spring-boot-starter-thymeleaf
dependency is all you need to integrate Thymeleaf with your Spring Boot project.
3. Create a Controller Class
In Spring Boot, controllers are responsible for handling HTTP requests and returning the appropriate views. Let’s create a simple controller class that will return a Thymeleaf view.
In this example:
- We are defining a controller method
home()
that maps to the root URL ("/"
). - The
message
attribute is added to the model, which will be rendered in the Thymeleaf template. - The return value
"home"
corresponds to thehome.html
template that will be placed in theresources/templates/
directory.
4. Create Thymeleaf Template (home.html
)
Next, you need to create a Thymeleaf template that will be used to render the view. Create a new HTML file in the src/main/resources/templates
directory called home.html
.
- The
th:text="${message}"
expression is a Thymeleaf syntax used to insert the value of themessage
attribute into the HTML. This value is passed from the controller.
5. Configure Thymeleaf (Optional)
Thymeleaf configuration is mostly automatic in Spring Boot. However, you can customize it further in application.properties
if needed.
For example, you can configure the suffix for Thymeleaf templates:
This ensures that Thymeleaf looks for .html
files in the templates directory by default.
You can also configure caching and other features for Thymeleaf:
6. Run the Application
Now, you can run the application. If you are using Spring Boot’s embedded Tomcat server, simply run the main()
method in your Application
class (it’s usually automatically generated when you create a Spring Boot project).
You can also run the application from the terminal:
Once the application is running, visit http://localhost:8080/
in your web browser. You should see the message rendered from the home.html
template.
Practical Example: Dynamic Data in Thymeleaf
You can easily pass more complex objects or collections to the Thymeleaf template. Let’s update the example to display a list of users dynamically.
1. Update the Controller
2. Update the Thymeleaf Template (home.html
)
In this example:
- The
users
list is passed to the model and displayed using ath:each
loop, which iterates over the list and generates a list of user names.
Conclusion
Creating a Spring Boot application with Thymeleaf for rendering views is straightforward and allows you to easily display dynamic content. By integrating Thymeleaf with Spring Boot, you can build powerful, scalable web applications that separate logic from presentation, making it easier to manage and maintain your application. With basic setup, controller integration, and template rendering, you’re all set to create robust web pages in no time.