Hackforge Academy

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";
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community