Category: java
Thymeleaf spring boot
Published on 06 Apr 2026
Explanation
Thymeleaf is a Java-based server-side
template engine
used in Spring Boot to create dynamic
web pages. It allows developers to combine
HTML with backend data and render views
before sending them to the browser.
Code:
// Example Controller returning
//Thymeleaf view
@GetMapping("/home")
public String home() {
return "home";
}
Explanation
Thymeleaf templates are stored inside
the templates
folder in Spring Boot and usually have
.html extension.
Code:
src/main/resources/templates/home.html
Explanation
Dynamic values from the controller can be
passed to Thymeleaf templates using
the Model
object.
Code:
@GetMapping("/welcome")
public String welcome(Model model) {
model.addAttribute("name", "Praveen");
return "welcome";
}
Explanation
Inside the Thymeleaf HTML file, values
passed
from the controller can be displayed using
th:text attribute.
Code:
<h1 th:text="${name}"></h1>
Explanation
Thymeleaf supports conditions, loops,
and dynamic rendering
similar to frontend frameworks
but executed on the server side.
Code:
<ul>
<li th:each="student : ${students}"
th:text="${student}"></li>
</ul>
Explanation
Thymeleaf is commonly used in Spring Boot
MVC applications to build login forms,
dashboards,
and admin panels where server-side
rendering is required instead of
REST APIs with React or Angular π.
Code:
@GetMapping("/students")
public String students(Model model) {
model.addAttribute("students",
List.of("Arun", "Divya", "Rahul"));
return "students";
}