Using Spring Boot with Thymeleaf for Dynamic Web Applications

Building modern web applications often requires the ability to generate dynamic content based on user input and application state. Thymeleaf is a popular Java-based templating engine that integrates seamlessly with Spring Boot, enabling developers to create rich web user interfaces. This post will guide you through the integration of Thymeleaf with Spring Boot to build dynamic web applications.

What is Thymeleaf?

Thymeleaf is a modern server-side Java template engine for web and standalone environments. It is designed to process and create HTML, XML, JavaScript, CSS, and text. Thymeleaf focuses on natural templating, allowing you to work with templates while maintaining their static appearance and usability in any environment.

Setting Up Thymeleaf in Spring Boot

To get started with Thymeleaf in a Spring Boot application, follow these steps:

1. Creating a Spring Boot Project

Use Spring Initializr to generate a new Spring Boot project. Include the following dependencies:

  • Spring Web
  • Thymeleaf

2. Adding the Thymeleaf Dependency

If you are manually adding the dependencies, include Thymeleaf in your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3. Creating HTML Templates

Create a directory named templates under src/main/resources, where your Thymeleaf HTML templates will reside. For example, create a file named index.html with the following content:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Welcome Page</title>
</head>
<body>
    <h1>Welcome, <span th:text="${userName}"></span>!</h1>
    <p>Here are some of our services:</p>
    <ul>
        <li>Service 1</li>
        <li>Service 2</li>
        <li>Service 3</li>
    </ul>
</body>
</html>

Creating a Controller

You need a Spring Controller to handle incoming requests and to serve the Thymeleaf templates. Create a controller class:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("userName", "John Doe");
        return "index"; // This references the index.html template
    }
}

Running Your Application

Run your Spring Boot application, and navigate to http://localhost:8080. You should see a welcome message with the username populated from the model:

Welcome, John Doe!

Debugging Thymeleaf Templates

While developing, you may want to enable debugging for Thymeleaf to see how the templates are being rendered and to check for potential issues in your template files. You can configure this in your application.properties file:

spring.thymeleaf.cache=false

Conclusion

Integrating Thymeleaf with Spring Boot makes it easy to create dynamic web applications that are both powerful and user-friendly. This combination allows you to separate your application’s logic from its presentation, following the MVC architecture effectively.

For further learning, including advanced templates and integrations, feel free to explore the extensive resources provided by ITER Academy to help you enhance your Spring development skills.

To learn more about ITER Academy, visit our website.

Scroll to Top