📘 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, you will learn five types of Thymeleaf Standard Expressions - Variable, Selection, Message, Link, and Fragment expressions.
Check out the complete Thymeleaf tutorials and examples at Thymeleaf Tutorial
Thymeleaf Standard Expressions
- ${...}: Variable expressions
- *{...} : Selection expressions
- #{...} : Message (i18n) expressions
- @{...} : Link (URL) expressions
- ~{...}: Fragment expressions
1. Variable Expressions
${VariableName}
1.1 Variable Expressions Example
<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>
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;
}
}
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 variable-expression request
@GetMapping("variable-expression")
public String variableExpression(Model model){
User user = new User("Ramesh", "ramesh@gmail.com", "ADMIN", "Male");
model.addAttribute("user", user);
return "variable-expression";
}
}
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
>
<head>
<meta charset="UTF-8">
<title>Variable Expressions</title>
</head>
<body>
<h1>Variable Expression Demo:</h1>
<h2>User Details:</h2>
<div>
<p> Name: <strong th:text="${user.name}"></strong></p>
<p> Email: <strong th:text="${user.email}"></strong></p>
<p> Role: <strong th:text="${user.role}"></strong></p>
<p> Gender: <strong th:text="${user.gender}"></strong></p>
</div>
</body>
</html>
Demo:
2. Selection Expressions
2.1 Selection Expressions Example
// handler method to handle selection expression
// http://localhost:8080/selection-expression
@GetMapping("selection-expression")
public String selectionExpression(Model model){
User user = new User("Ramesh", "ramesh@gmail.com", "ADMIN", "Male");
model.addAttribute("user", user);
return "selection-expression";
}
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
>
<head>
<meta charset="UTF-8">
<title>Selection Expressions</title>
</head>
<body>
<h1>Selection Expressions Demo</h1>
<h2>User Details:</h2>
<div th:object="${user}">
<p> Name: <strong th:text="*{name}"></strong></p>
<p> Email: <strong th:text="*{email}"></strong></p>
<p> Role: <strong th:text="*{role}"></strong></p>
<p> Gender: <strong th:text="*{gender}"></strong></p>
</div>
</body>
</html>
3. Message Expressions
#{message.property.key}
3.1 Message Expressions Example
app.name=Spring Boot Thymeleaf Application
welcome.message=Hello, welcome to Spring boot application
// handler method to handle message expressions request
// http://localhost:8080/message-expression
@GetMapping("message-expression")
public String messageExpression(){
return "message-expression";
}
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymelaf.org"
>
<head>
<meta charset="UTF-8">
<title>Message Expressions</title>
</head>
<body>
<h1>Message Expressions Demo:</h1>
<h2 th:text="#{app.name}"></h2>
<h2 th:text="#{welcome.message}"></h2>
</body>
</html>
4. Link Expressions
@{link}
4.1 Link Expressions Example
// handler method to handle link expressions
// http://localhost:8080/link-expression
@GetMapping("link-expression")
public String linkExpression(Model model){
model.addAttribute("id", 1);
return "link-expression";
}
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
>
<head>
<meta charset="UTF-8">
<title>Link Expressions</title>
<link th:href="@{/css/demo.css}" rel="stylesheet" />
</head>
<body>
<h1>Link Expressions Demo:</h1>
<a th:href="@{/variable-expression}"> variable-expression </a>
<a th:href="@{/selection-expression}"> selection-expression </a>
<p> <a th:href="@{link-expression/{id}(id=${id})}">link with parameter</a></p>
</body>
</html>
5. Fragment Expressions
~{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
5.1 Fragment Expressions Example
<!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>
// handler method to handle fragment expression
@GetMapping("fragment-expression")
public String fragmentExpression(){
return "fragment-expression";
}
<!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>
Conclusion
Related Thymeleaf Tutorials and Examples
- Introducing Thymeleaf | Thymeleaf Template | Thymeleaf Template Engine
- Thymeleaf Example with Spring Boot
- 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