Java ThymileAF:检查是否定义了变量
如何检查变量是否定义在Thymeleaf中 Javascript中的类似内容:Java ThymileAF:检查是否定义了变量,java,spring,spring-mvc,spring-boot,thymeleaf,Java,Spring,Spring Mvc,Spring Boot,Thymeleaf,如何检查变量是否定义在Thymeleaf中 Javascript中的类似内容: if (typeof variable !== 'undefined') { } 或者在PHP中: if (isset($var)) { } Thymeleaf中是否有等效属性?是的,您可以使用以下代码轻松检查文档是否存在给定属性。请注意,如果满足以下条件,您将创建div标记: <div th:if="${variable != null}" th:text="Yes, variable exists!"&
if (typeof variable !== 'undefined') { }
或者在PHP中:
if (isset($var)) { }
Thymeleaf中是否有等效属性?是的,您可以使用以下代码轻松检查文档是否存在给定属性。请注意,如果满足以下条件,您将创建
div
标记:
<div th:if="${variable != null}" th:text="Yes, variable exists!">
I wonder, if variable exists...
</div>
甚至更短,不使用if语句
<div th:text="${variable?.name}">
I wonder, if variable.name exists...
</div>`
我想知道variable.name是否存在。。。
`
但是使用此语句,您将结束创建div
标记,无论variable
或variable.name
是否存在
您可以了解有关thymeleaf缩写形式中条件的更多信息:
姓名:
姓名:
为了判断上下文是否包含给定变量,可以直接询问上下文变量映射。这使我们可以确定是否指定了变量,而不是仅在定义了变量但其值为null的情况下
胸腺素2
使用对象的方法:
可以使用条件运算符。如果存在或为空字符串,则将写入变量:
<p th:text="${variable}?:''"></p>
较短的示例非常完美!对于对象,可以像那样使用if
。如果变量
是一个值为0
的整数
,thymeleaf将其视为null
,如果code,则不输入de。
<div th:if="${#vars.containsKey('myVariable')}" th:text="Yes, $myVariable exists!"></div>
<div th:if="${#ctx.containsVariable('myVariable')}" th:text="Yes, $myVariable exists!"></div>
<p th:text="${variable}?:''"></p>