📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ 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 learn how to use Thymeleaf Fragment Expression in Thymeleaf HTML templates with an example.
Check out the complete Thymeleaf tutorials and examples at Thymeleaf Tutorial
~{fragment name}
- th:insert – inserts content inside the tag
- th:replace – replaces the current tag with the tag defining the fragment
- th:include – this is deprecated but it may still appear in a legacy code // don't use it
<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>
User Model Class
package net.javaguides.thymeleaf.model; public class User { private String name; private String email; private String role; private String gender; public User(String name, String email, String role, String gender) { this.name = name; this.email = email; this.role = role; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
Create header and footer Fragments
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>Header</title> </head> <body> <div th:fragment="header"> <h1> Header Part</h1> <hr /> </div> </body> </html>
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>Footer</title> </head> <body> <div th:fragment="footer"> <hr /> <h1>Footer Part</h1> </div> </body> </html>
Spring MVC Controller - UserController
Let's create a UserController and add the handler method to return the Thymeleaf template like:package net.javaguides.thymeleaf.controller; import net.javaguides.thymeleaf.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class UserController { // handler method to handle fragment expression @GetMapping("fragment-expression") public String fragmentExpression(){ return "fragment-expression"; } }
Thymeleaf Template: fragment-expression.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" > <head> <meta charset="UTF-8"> <title>Fragment Expression</title> </head> <body> <h1> Fragment Expressions Demo:</h1> <div th:replace="~{common/header :: header}"></div> <div> <h1>Page Body</h1> </div> <div th:replace="~{common/footer :: footer}"></div> </body> </html>
Demo
Related Thymeleaf Examples
- Thymeleaf Standard Expressions
- Thymeleaf th:text Attribute
- Thymeleaf Variable Expression
- Thymeleaf Selection Expression
- Thymeleaf Message Expression
- Thymeleaf Link Expression
- Thymeleaf Fragment Expression
- Introducing Thymeleaf | Thymeleaf Template | Thymeleaf Template Engine
- Thymeleaf Example with Spring Boot
- Spring Boot 3 Thymeleaf Example
- How to Add CSS and JS to Thymeleaf
- Add Bootstrap CSS to Thymeleaf
- How to handle null values in Thymeleaf?
- How to Loop a List by Index in Thymeleaf
- Thymeleaf Array Example - Array Index, Array Iteration
- Thymeleaf Enums Example
- Thymeleaf If Else Condition Example
- Thymeleaf Switch Case Example
Comments
Post a Comment
Leave Comment