thymeleaf html if else
时间: 2023-10-26 19:05:07 浏览: 172
thymeleaf文档
Thymeleaf provides conditional statements like `if` and `else` to handle conditional rendering of HTML elements.
Here's an example of using `if` and `else` in Thymeleaf:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Conditional Rendering with Thymeleaf</title>
</head>
<body>
<div th:if="${user.isAdmin}">
<h1>Welcome Admin!</h1>
</div>
<div th:else>
<h1>Welcome User!</h1>
</div>
</body>
</html>
```
In the above example, if the `user.isAdmin` is true, then the `h1` tag inside the first `div` will be rendered. Otherwise, the `h1` tag inside the second `div` will be rendered.
You can also use the shorthand notation for the `if` statement as shown below:
```
<div th:if="${user.isAdmin}">Welcome Admin!</div>
```
This will render the `div` tag with the "Welcome Admin!" text only if the `user.isAdmin` is true.
阅读全文