Thymeleaf:检查是否定义了变量

Thymeleaf:检查是否定义了变量,第1张

Thymeleaf:检查是否定义了变量

是的,您可以使用以下代码轻松检查文档的给定属性是否存在。请注意,

div
如果符合条件,您将创建标签:

<div th:if="${variable != null}" th:text="Yes, variable exists!">   I wonder, if variable exists...</div>

如果您想使用

variable
字段,则值得检查此字段是否也存在

<div th:if="${variable != null && variable.name != null}" th:text="${variable.name}">   I wonder, if variable.name exists...</div>

甚至更短,无需使用if语句

<div th:text="${variable?.name}">   I wonder, if variable.name exists...</div>`

但是,使用这种说法你会最终创建

div
标签是否
variable
还是
variable.name
存在

您可以在此处了解更多关于百里香中条件性的信息



欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5621685.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存