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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.0-M4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>net.javaguides</groupId>
	<artifactId>springboot-thymeleaf-tutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-thymeleaf-tutorial</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>

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

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

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;

@Controller
public class HelloWorldController {

    // handler method to handle /helloworld request
    // http://localhost:8080/hello-world
    @GetMapping("/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"
    xmlns:th="http://www.thymeleaf.org"
>
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf Hello World Example</title>
</head>
<body>
    <h2 th:text="${message}">Hello Good Morning!.</h2>
</body>
</html>

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;

@SpringBootApplication
public class ThymeleafSpringBootTutorialApplication {

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

}

Demo

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

http://localhost:8080/hello-world

Conclusion

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.

Comments