Spring Boot is a powerful Java-based framework that simplifies the process of developing stand-alone, production-grade applications. Thymeleaf is a server-side Java template engine that enables developers to create elegant and maintainable HTML templates. In this tutorial, we will guide you through the process of setting up a simple Spring Boot project using Thymeleaf for seamless web development.
Step 1: Installing the Required Tools
Before we begin, you need to have the following tools installed on your system:
- Java Development Kit (JDK) 8 or later
- Apache Maven
- Integrated Development Environment (IDE) of your choice (e.g., IntelliJ IDEA, Eclipse, or VS Code)
Step 2: Creating a New Spring Boot Project
To create a new Spring Boot project, open your IDE and follow these steps:
- Create a new Maven project.
- Set the “Group ID” and “Artifact ID” as per your project requirements.
- Choose “Spring Boot” as the project type.
- Select “Web” as the project’s primary dependency.
Step 3: Adding Thymeleaf Dependency
To use Thymeleaf in your Spring Boot project, add the following dependency to your pom.xml
file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Step 4: Configuring Thymeleaf
In your src/main/resources
folder, create a new folder named templates
. This is where you will store your Thymeleaf template files.
Open the src/main/resources/application.properties
file and add the following configuration properties:
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
Step 5: Creating a Simple Controller
Create a new Java class in your project’s src/main/java
folder and name it HomeController
. Add the following code:
package com.example.demo;
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("message", "Welcome to our Spring Boot Thymeleaf tutorial!");
return "home";
}
}
Step 6: Creating a Thymeleaf Template
Create a new HTML file named home.html
in the templates
folder and add the following code:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot Thymeleaf Tutorial</title>
</head>
<body>
<h1 th:text="${message}">Welcome message</h1>
</body>
</html>
Step 7: Running the Application
Run your Spring Boot application by executing the main
method in your project’s main class (usually named DemoApplication
). Open your browser and navigate to http://localhost:8080
to see your Thymeleaf template in action.
Conclusion
You have now successfully set up a simple Spring Boot project using Thymeleaf. This basic example demonstrates the integration of Thymeleaf with Spring Boot, allowing you to create dynamic and maintainable web applications with ease. As you become more familiar with these tools, you can expand your knowledge and explore more advanced features to create complex and powerful applications. Keep experimenting and building on your skills to become a proficient Spring Boot and Thymeleaf developer. Happy coding!