您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Thymeleaf:检查是否定义了变量

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存在

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

其他 2022/1/1 18:20:19 有366人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶