In this example, we will discuss how to perform if-else condition in Thymeleaf with an example.
Learn Thymeleaf at https://www.javaguides.net/p/thymeleaf-tutorial.html
Using Switch Case Statement
To achieve similar to if-else condition in Thymeleaf we can use th:switch attribute.
This command is equivalent to a switch structure in Java. The following example shows how to use it:
<div th:switch="${condition}">
<p th:case="${true}">TRUE</p>
<p th:case="*">FALSE</p>
</div>
Thymeleaf at the first step will evaluate ${condition} expression and if the value is true it will print p tag with TRUE text. When an evaluated value is different than the true the Thymeleaf engine will generate p element with FALSE.
The default option for th:switch is specified as th:case="*". This simple switch with default handled will work as if-else command.
Thymeleaf th:switch, th:case Attributes Example with Spring boot
In this example, we will demonstrate the usage of th:switch, th:case attributes.
Let's add below dependency to integrate Thymeleaf with Spring boot:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Let's create a User class:
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;
}
}
Now, let's create Spring MVC controller with handler method to return Thymeleaf template like:
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";
}
}
Here is the Thymeleaf template to demonstrates the use of a switch case statement:
<!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>
<!-- * for default case -->
<p th:case="*">User is some other thing</p>
</div>
</div>
</div>
</body>
</html>
If the User role is "Admin" then "User is an administrator" text prints on the web page.
The th:case = "*" is the default case of the th:swith/th:case structure. If all the above cases are evaluated as false the code of default case will be "rendered".
Using th:if and th:unless attributes
Thymeleaf also provides an inverse attribute of th:if which is th:unless. We can use it to construct conditional sections that work like an if-else command.
<div th:if="${condition}">
<p>TRUE</p>
</div>
<div th:unless="${condition}">
<p>FALSE</p>
</div>
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