And*_*rea 27 java spring spring-mvc thymeleaf spring-boot
我如何检查是否变量定义在Thymeleaf?
在Javascript中有类似的东西:
if (typeof variable !== 'undefined') { }
Run Code Online (Sandbox Code Playgroud)
或者这在PHP中:
if (isset($var)) { }
Run Code Online (Sandbox Code Playgroud)
在Thymeleaf中有相同的东西吗?
Lay*_*ros 10
简写:
<div th:if="${currentUser}">
<h3>Name:</h3><h3 th:text="${currentUser.id}"></h3>
<h3>Name:</h3><h3 th:text="${currentUser.username}"></h3>
</div>
Run Code Online (Sandbox Code Playgroud)
小智 6
为了判断上下文是否包含给定变量,您可以直接询问上下文变量映射。这样一来,就可以确定是否完全指定了变量,这与仅定义了变量但其值为null的情况相反。
使用#vars对象的containsKey方法:
<div th:if="${#vars.containsKey('myVariable')}" th:text="Yes, $myVariable exists!"></div>
Run Code Online (Sandbox Code Playgroud)
使用#ctx对象的containsVariable方法:
<div th:if="${#ctx.containsVariable('myVariable')}" th:text="Yes, $myVariable exists!"></div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
56352 次 |
| 最近记录: |