Category: Spring Updated: Jul 11, 2026

Spring Boot Thymeleaf Tutorial for Beginners: How to Use Thymeleaf in Java Web Development

Spring Boot Thymeleaf Tutorial for Beginners: How to Use Thymeleaf
Spring Boot Thymeleaf Tutorial for Beginners: How to Use Thymeleaf

Learn Through a Conversation Between a Teacher and a Student

Student

"I am learning Spring Boot, but I do not understand how to display data on a web page."

Teacher

"In Spring Boot, you can use Thymeleaf as a template engine to display data dynamically."

Student

"What is a template engine, and why do I need it?"

Teacher

"A template engine connects your Java backend with HTML, so you can show dynamic content."

Student

"Can I learn it step by step as a beginner?"

Teacher

"Yes, let’s go through Thymeleaf basics so you can build real web pages with Spring Boot."

1. What Is Thymeleaf

Key Takeaway: Let's dive into the core concepts of "1. What Is Thymeleaf" cleanly and effectively.

1. What Is Thymeleaf
1. What Is Thymeleaf

Thymeleaf is a popular template engine used in Spring Boot applications. It allows developers to create dynamic web pages using HTML combined with Java data.

For Java beginners and those searching for Java tutorial for beginners, Thymeleaf is easy to understand because it uses standard HTML syntax. This makes it a great choice for building web applications without complex frontend frameworks.

In Java backend development, Thymeleaf plays an important role in connecting the controller layer and the view layer.

2. Setting Up Thymeleaf in Spring Boot

2. Setting Up Thymeleaf in Spring Boot
2. Setting Up Thymeleaf in Spring Boot

To use Thymeleaf, you need to add the dependency to your project. This is usually done using Maven or Gradle.


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

After adding this dependency, Spring Boot automatically configures Thymeleaf for you. This is one reason why Spring Boot is widely used in Java programming basics and backend tutorials.

3. Creating a Simple Controller

3. Creating a Simple Controller
3. Creating a Simple Controller

In Spring Boot, a controller handles requests and sends data to the view. You can create a simple controller to pass data to a Thymeleaf template.


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

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("message", "Hello from Spring Boot!");
        return "hello";
    }
}

This controller sends a message to the HTML page. The view name hello corresponds to a Thymeleaf template file.

4. Creating a Thymeleaf Template

Key Takeaway: Let's dive into the core concepts of "4. Creating a Thymeleaf Template" cleanly and effectively.

4. Creating a Thymeleaf Template
4. Creating a Thymeleaf Template

Thymeleaf templates are placed in the templates folder. You can create an HTML file to display data from the controller.


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf Example</title>
</head>
<body>
    <h1 th:text="${message}">Default Message</h1>
</body>
</html>

The attribute th:text is used to display dynamic data. This replaces the default text with the value from the controller.

5. Displaying Lists and Looping Data

5. Displaying Lists and Looping Data
5. Displaying Lists and Looping Data

Thymeleaf makes it easy to display lists of data using loops. This is useful when working with database results or collections.


@GetMapping("/list")
public String list(Model model) {
    model.addAttribute("items", java.util.Arrays.asList("Apple", "Banana", "Orange"));
    return "list";
}

<ul>
    <li th:each="item : ${items}" th:text="${item}"></li>
</ul>

The th:each attribute is used to iterate over the list. This is commonly used in Java backend development and Spring Boot tutorials.

6. Form Handling with Thymeleaf

6. Form Handling with Thymeleaf
6. Form Handling with Thymeleaf

Thymeleaf is also useful for handling form input. You can create forms that send data back to the server.


<form action="/submit" method="post">
    <input type="text" name="name" />
    <button type="submit">Submit</button>
</form>

This allows users to interact with your application. Handling form data is an essential skill in Java web development.

7. Conditional Rendering

Key Takeaway: Let's dive into the core concepts of "7. Conditional Rendering" cleanly and effectively.

7. Conditional Rendering
7. Conditional Rendering

Thymeleaf allows you to display content conditionally. This is useful when you want to show or hide elements based on data.


<p th:if="${message != null}" th:text="${message}"></p>

The th:if attribute controls whether the element is displayed. This is helpful for building dynamic user interfaces.

8. Best Practices for Beginners

8. Best Practices for Beginners
8. Best Practices for Beginners

When learning Thymeleaf, start with simple examples and gradually build more complex applications. Practice creating controllers, templates, and forms.

Keep your HTML clean and separate business logic from the view. This approach follows best practices in Java backend development.

By mastering Thymeleaf, you can build dynamic web applications using Spring Boot and improve your Java programming skills.

Summary

Summary
Summary

In this Spring Boot Thymeleaf tutorial for beginners, we explored how to connect a Java backend with HTML views using a template engine. Thymeleaf plays an important role in Java web development because it allows developers to display dynamic data easily without complex frontend frameworks. For those learning Java programming basics or searching for a Java tutorial for beginners, understanding how Thymeleaf works is a major step toward building real applications.

We started by understanding what Thymeleaf is and why it is widely used in Spring Boot applications. Unlike other template engines, Thymeleaf uses natural HTML syntax, which makes it easier for beginners to read and maintain. This means you can open a Thymeleaf file in a browser and still see a meaningful layout even without a running backend. This feature makes it very beginner friendly and suitable for developers who are just starting with Java backend development.

We also learned how to set up Thymeleaf in a Spring Boot project by adding the necessary dependency. Once the dependency is included, Spring Boot automatically configures everything, which reduces the need for manual setup. This simplicity is one of the reasons why Spring Boot is so popular among Java beginners and professionals alike.

Creating a controller was another important topic. In Spring Boot, controllers are responsible for handling requests and passing data to the view layer. By using a simple controller, we were able to send a message to a Thymeleaf template and display it on a web page. This demonstrates how backend logic and frontend display are connected in a typical Java web application.

We then created a Thymeleaf template and used attributes such as th text to display dynamic data. This is one of the most basic and essential features of Thymeleaf. Once you understand this concept, you can move on to more advanced features such as loops, conditions, and form handling.

Displaying lists and looping data using th each is another powerful feature. It allows developers to render multiple items easily, which is useful when working with database results. Many real world applications require displaying lists such as products, users, or messages, so this skill is very important.

Form handling is also a key part of web development. Using Thymeleaf, you can create forms that send data to the server and process user input. This interaction between the user and the system is what makes web applications dynamic and useful. Beginners often search for how to handle forms in Spring Boot, and Thymeleaf provides a simple solution.

Conditional rendering allows you to control what is displayed based on data. This makes your application more flexible and user friendly. By combining conditions with loops and variables, you can build complex interfaces with minimal effort.

Finally, we discussed best practices such as keeping your templates clean and separating business logic from the view. This is an important principle in Java backend development and helps maintain code quality as your project grows. By following these practices, you can create scalable and maintainable applications.

Overall, Thymeleaf is an essential tool for anyone learning Spring Boot. It provides a simple yet powerful way to build dynamic web pages and connect Java backend logic with HTML. By mastering Thymeleaf, you can take your Java development skills to the next level and build real world applications with confidence.

Sample Program: Display Multiple Values


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

@Controller
public class MultiController {

    @GetMapping("/multi")
    public String multi(Model model) {
        model.addAttribute("name", "Alice");
        model.addAttribute("age", 25);
        return "multi";
    }
}

<p th:text="${name}"></p>
<p th:text="${age}"></p>

Sample Program: Conditional Display


@GetMapping("/status")
public String status(Model model) {
    model.addAttribute("active", true);
    return "status";
}

<p th:if="${active}">Active User</p>
<p th:unless="${active}">Inactive User</p>

Sample Program: Simple Form Processing


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@PostMapping("/submit")
public String submit(@RequestParam String name, Model model) {
    model.addAttribute("result", "Hello " + name);
    return "result";
}

<form action="/submit" method="post">
    <input type="text" name="name" />
    <button type="submit">Submit</button>
</form>
<p th:text="${result}"></p>
Review Conversation Between the Teacher and Student

Student

"I understand now how Thymeleaf connects Java and HTML."

Teacher

"Yes, it is a key part of Spring Boot web development."

Student

"The examples with lists and conditions were very helpful."

Teacher

"Those features are used in almost every real application."

Student

"I want to build a simple web app using these techniques."

Teacher

"That is a great next step. Practice will help you improve quickly."

Student

"Now I feel more confident about Spring Boot and Thymeleaf."

Teacher

"Keep building projects and you will master Java web development."

Back to Category
Latest Articles
New1
Spring
Spring Boot and Java Version Compatibility Guide: Spring Boot 3.5 3.4 3.3 with Java 21 and Java 17
New
New2
Spring
Complete Guide to Spring Data JPA getReferenceById for Beginners
New
New3
Spring
Complete Guide to Spring Data JPA findAll for Beginners
New
New4
Spring
Spring Data JPA JpaRepository Tutorial for Beginners: Complete Guide to Database Access in Spring Boot
New
Popular Articles
No.1
Java&Spring記事人気No1
Spring
Complete Guide to Spring @PreAuthorize for Java Beginners – Secure Your Application with Method-Level Security
No.2
Java&Spring記事人気No2
Spring
Complete Guide to Spring @GeneratedValue for Java Beginners – Understand Primary Key Generation in JPA
No.3
Java&Spring記事人気No3
Spring
Spring Boot GetMapping Tutorial: Complete Guide to GetMapping in Java for Beginners
No.4
Java&Spring記事人気No4
Spring
Spring @Valid Annotation Tutorial for Beginners: Input Validation in Spring Boot
No.5
Java&Spring記事人気No5
Spring
Spring Data JPA Like Query Guide: findByFirstnameLike for Java Beginners
No.6
Java&Spring記事人気No6
Spring
Mastering the @PostMapping Annotation in Java Spring: A Complete Guide for Beginners
No.7
Java&Spring記事人気No7
Spring
Mastering the @Id Annotation in Java Spring: A Complete Guide for Beginners
No.8
Java&Spring記事人気No8
Spring
Mastering the @Entity Annotation in Java Spring: A Complete Guide for Beginners