Spring Boot 3 Thymeleaf Example

In this tutorial, we will create a Spring boot Thymeleaf Hello World example using the latest version of Spring boot 3 and Thymeleaf.

Check out the complete Thymeleaf tutorials and examples at Thymeleaf Tutorial

Spring Boot aims to make it easy to create Spring-powered, production-grade applications and services with minimum fuss. It takes an opinionated view of the Spring platform so that new and existing users can quickly get to the bits they need.
Thymeleaf is a Java-based library used to create a web application. It provides good support for serving an XHTML/HTML5 in web applications.

1. Create a Spring Boot Application

Let's create a Spring boot project using the spring initializr.

Refer to below screenshot to enter details while creating the spring boot application.

2. Maven pom.xml File

Here is the Maven pom.xml file for your reference:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>


			<name>Spring Milestones</name>
			<name>Spring Milestones</name>


Note that we have added the below dependency to integrate Thymeleaf in the Spring boot project:


Spring MVC Controller - HelloWorldController.java

Now, let's create a Spring MVC controller (HelloWorldController) with handler method to return Thymeleaf template like:
package net.javaguides.thymeleaf.controller;

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

public class HelloWorldController {

    // handler method to handle /helloworld request
    // http://localhost:8080/hello-world
    public String helloWorld(Model model){
        model.addAttribute("message", "Hello World!");
        return "hello-world";

Thymeleaf ViewResolver Configuration

Spring boot will auto-configure view resolver for Thymeleaf whenever it will find the springboot-thymeleaf-starter dependency on the classpath hence we don't have to manually configure ViewResolver for Thymeleaf.
Spring boot will pick Thymeleaf templates (HTML pages) from the resources/templates folder.

Thymeleaf Template - hello-world.html

Here is the Thymeleaf template to display the "Hello World!" message:

<!DOCTYPE html>
<html lang="en"
    <meta charset="UTF-8">
    <title>Thymeleaf Hello World Example</title>
    <h2 th:text="${message}">Hello Good Morning!.</h2>

The th:text attribute evaluates variable expressions and adds the result as the body of the <h2> tag.

Run Spring Boot Application and Have a Demo

Let's run this spring boot application from IDE. Go to the main entry point class and right-click -> Run As -> Java Application:
package net.javaguides.thymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class ThymeleafSpringBootTutorialApplication {

	public static void main(String[] args) {
		SpringApplication.run(ThymeleafSpringBootTutorialApplication.class, args);



Once the Spring boot application is up and running, go to a browser and hit the below link in the browser:



In this tutorial, we have seen how to create a simple Spring boot Thymeleaf Hello World example using the latest version of Spring boot 3 and Thymeleaf.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course