🌿 What is Thymeleaf?

Thymeleaf is a template engine – it helps you create dynamic HTML pages in a Spring Boot application.

Think of it like this:

Just like MS Word uses templates to generate documents, Thymeleaf helps generate web pages by filling in data automatically.


šŸ›  How it works with Spring Boot?

  1. You create an HTML page with Thymeleaf tags (like ${name}).
  2. Spring Boot sends data from your Java code (Controller) to the HTML page.
  3. Thymeleaf replaces those tags with real data and shows the final page to the user.

šŸ“¦ Example

Controller (Java code):

@GetMapping("/greet")
public String greet(Model model) {
Ā  Ā  model.addAttribute("name", "Aftab");
Ā  Ā  return "greeting";
}
Ā 

Thymeleaf HTML (greeting.html):

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
Ā  Ā  <h1>Hello, <span th:text="${name}"></span>!</h1>
</body>
</html>

Ā 

šŸ–„ļø Output:
Hello, Aftab!


āœ… Why use Thymeleaf?

  • Easy to learn
  • Works well with Spring Boot
  • Helps build UI with real-time data
  • Supports loops, conditions, and formatting
Back to blog

Leave a comment