📘 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
Learn Thymeleaf at https://www.javaguides.net/p/thymeleaf-tutorial.html
If – Unless (th:if, th:unless Attributes)
Syntax for th:if Attribute
<div th:if="condition">
<!-- Other code -->
</div>
Syntax for th:unless Attribute
<button th:unless = "condition"> ... </button>
<!-- Same as -->
<button th:if = "!condition"> ... </button>
Thymeleaf th:if, th:unless Attributes Example with Spring boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
package net.javaguides.springboot;
public class User {
private String userName;
private String email;
private String role;
public User(String userName, String email, String role) {
super();
this.userName = userName;
this.email = email;
this.role = role;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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;
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class UserController {
@GetMapping("/if-unless")
public String users(Model model) {
List < User > users = new ArrayList < > ();
users.add(new User("Ramesh", "ramesh@gmail.com", "ADMIN"));
users.add(new User("Tom", "tom@gmail.com", "USER"));
users.add(new User("John", "john@gmail.com", "USER"));
model.addAttribute("users", users);
return "if-unless";
}
}
<a class="btn btn-primary" th:if="${user.role} == 'ADMIN'">Update</a>
<a class="btn btn-danger" th:if="${user.role} == 'ADMIN'">Delete</a>
<a class="btn btn-primary" th:unless="${user.role} == 'ADMIN'">View</a>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>if and unless attribute Demo</title>
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row">
<h1>Employees</h1>
<table class="table">
<thead>
<tr>
<th>User Name</th>
<th>User Email</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.userName}"></td>
<td th:text="${user.email}"></td>
<td><a class="btn btn-primary" th:if="${user.role} == 'ADMIN'">Update</a>
<a class="btn btn-danger" th:if="${user.role} == 'ADMIN'">Delete</a>
<a class="btn btn-primary" th:unless="${user.role} == 'ADMIN'">View</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Thymeleaf th:switch, th:case Attributes
Thymeleaf th:switch, th:case Attributes Example with Spring boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
package net.javaguides.springboot;
public class User {
private String userName;
private String email;
private String role;
public User(String userName, String email, String role) {
super();
this.userName = userName;
this.email = email;
this.role = role;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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;
}
}
package net.javaguides.springboot;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class UserController {
@GetMapping("/switch-case")
public String switchExample(Model model) {
User user = new User("Ramesh", "ramesh@gmail.com", "ADMIN");
model.addAttribute("user", user);
return "switch-case";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Thymeleaf switch case Demo</title>
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row">
<h1>Thymeleaf swith case demo</h1>
<h4 th:utext="${user.userName}"></h4>
<div th:switch="${user.role}">
<p th:case="'ADMIN'">User is an administrator</p>
<p th:case="'MANAGER'">User is a manager</p>
<p th:case="'GUEST'">User is a guest</p>
<!-- * for default case -->
<p th:case="*">User is some other thing</p>
</div>
</div>
</div>
</body>
</html>
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