是的,您可以使用以下代码轻松检查文档的给定属性是否存在。请注意,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
存在