Spring Boot Thymeleaf Tutorial With Example
Spring Boot Thymeleaf Tutorial With Example
HelloKoding
Guides Spring Boot JPA and Hibernate REST with Spring Security with Spring Java Data Structure Algorithm
Golang
# Spring Boot
This tutorial walks you through the steps of creating a Hello World web app example with Spring Boot
and Thymeleaf
Thymeleaf is a server-side Java template engine for both web and standalone environments
The web page accepts and shows the value of a query string parameter input from the user on the HTML
web page
Maven 3+
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 1/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
You can create and init a new Spring Boot project by using Spring Initializr or your IDE
Following is the final project structure with all the files we would create
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── hellokoding
│ │ └── springboot
│ │ └── view
│ │ ├── Application.java
│ │ └── HelloController.java
│ └── resources
│ ├── static
│ │ ├── css
│ │ │ └── main.css
│ │ └── js
│ │ └── main.js
│ ├── templates
│ │ └── hello.html
│ └── application.properties
└── pom.xml
pom.xml is the configuration file used by Maven to manage project dependencies and build process, it
is usually placed in the project root directory
Web controller classes are used for mapping user requests to Thymeleaf template files, would be created
inside src/main/java
Application.java is a launch file for Spring Boot to start the application, would be created inside
src/main/java
Project dependencies
For a Spring Boot Thymeleaf web application, we will need the following dependencies on the pom.xml
file
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 2/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
The library versions can be omitted as it will be resolved by the parent pom provided by Spring Boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Apart from that, we also use the spring-boot-devtools dependency to auto-trigger an application
restart or live reload in the development environment whenever Java class or static files on class-path
change, respectively. However, to leverage that, you need to configure your IDE to auto-save and auto-
compile when files are modified
In the production environment, when a Spring Boot application is launched from a jar file, the
devtools is auto disabled
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Create Controller
Create a Spring Boot controller file to map HTTP requests to Thymeleaf view template files
[HelloController.java]
package com.hellokoding.springboot.view;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 3/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
public class HelloController {
@GetMapping({"/", "/hello"})
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="Wo
model.addAttribute("name", name);
return "hello";
}
}
@GetMapping maps HTTP GET request for "/" (home page) and "/hello" to the hello method
Model is a Spring object for sharing data between handler and view template
The view template name is defined by the return statement of the handler and the
spring.thymeleaf.suffix config property which defined in the below
application.properties file. So in this hello handler method, the return view is
hello.html
[hello.html]
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="'Hello, ' + ${name} + '!'"></title>
<link href="/css/main.css" rel="stylesheet">
</head>
<body>
<h2 class="hello-title" th:text="'Hello, ' + ${name} + '!'"></h2>
<script src="/js/main.js"></script>
</body>
</html>
The dynamic message is ${name} . It is an Java Expression Language enabling Thymeleaf files to access
the data from the model. Its value is filled by the model.addAttribute("name", name); defined
in the above HelloController
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 4/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
Static files
Create 2 simple CSS and JavaScript files inside /src/main/resources/static
The main.css file is linked into Thymeleaf view via <link href="/css/main.css"
rel="stylesheet">
[main.css]
.hello-title{
color: darkgreen;
}
The main.js file is included into Thymeleaf view via <script src="/js/main.js">
</script>
[main.js]
(function(){
console.log("Hello World!");
})();
Application Configurations
Create application.properties file inside src/main/resources to configure Spring MVC
view resolver via the spring.mvc.view properties
[application.properties]
spring.thymeleaf.template-loader-path: classpath:/templates
spring.thymeleaf.suffix: .html
spring.thymeleaf.cache: false
Under the hood, Spring Boot will auto-configure Spring MVC view resolver based on the above settings
Create an Application class and use @SpringBootApplication annotation to launch the application
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 5/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
[Application.java]
package com.hellokoding.springboot.view;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run the application by typing the following command on the terminal console at the project root
directory
Hello, World!
Try to modify the Thymeleaf, CSS, and JavaScript files, and refresh the browser, the HTML response
would be updated accordingly thanks to the support from spring-boot-devtools
In a production environment, you may like to package and run the Spring Boot application as a single jar
file
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 6/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
Conclusion
In this tutorial, we learned to create a Hello World web application in Spring Boot with Thymeleaf. The
source code is available on Github
# Spring Boot
Share to social
Twitter Facebook
Van N.
Van N. is a software engineer, creator of HelloKoding. He loves coding, blogging, and traveling. You may find him
on GitHub and LinkedIn
Comments
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 7/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
17 Comments
1 Login
Name
YD Yesid Davila. − ⚑
7 years ago
Hello, I do the steps of the example and the project runs smoothly.
but when searching the path in the browser, the html view is not displayed.
instead, this message appears:
I repeat, the project runs smoothly. but it does not show the view.
4 4 Reply ⥅
0 0 Reply ⥅
Lukas Gužauskas − ⚑
5 years ago
Hello, I do the steps of the example and the project runs smoothly.
But the browser doesn't find /css/style.css. I get error page that htttp is not found 404
Сергей − ⚑
5 years ago
0 0 Reply ⥅
Ruan Nawê − ⚑
5 years ago
Excelent tutorial
0 0 Reply ⥅
F
Ferko − ⚑
5 years ago
0 0 Reply ⥅
I Ivana Milic − ⚑
5 years ago
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 8/12
I
19/12/2024 16:34
5 years ago
Spring Boot Thymeleaf Tutorial with Example
Thank you!
0 0 Reply ⥅
deadxperia − ⚑
5 years ago
It doesn't work. Author please update the repository or just delete this article, it's useless until you fix it.
0 0 Reply ⥅
S
starrychloe − ⚑
6 years ago
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might
not be accessible by any of the configured Template Resolvers
0 0 Reply ⥅
G
Got Motivation − ⚑
7 years ago
0 0 Reply ⥅
T Trnal − ⚑
7 years ago
0 0 Reply ⥅
Mathenge − ⚑
8 years ago
0 0 Reply ⥅
Kevin Yang − ⚑
8 years ago
0 0 Reply ⥅
WG Wael Gomaa
> Guest − ⚑
8 years ago
3 0 Reply ⥅
Wiktor Kalinowski − ⚑
7 years ago
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 9/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
Search ...
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 10/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 11/12
19/12/2024 16:34 Spring Boot Thymeleaf Tutorial with Example
Spring Boot
Java
Data Structure
Algorithm
Golang
GitHub
https://hellokoding.com/spring-boot-hello-world-example-with-thymeleaf/ 12/12