🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
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
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
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business


Comments
Post a Comment
Leave Comment