我想要的是 Thymeleaf 中 th:each 语句中的 if-else。
If currentSkill != null
,然后显示包含内容的表格,否则“您没有任何技能”
这是没有 if/else 的代码:
<div th:each="skill : ${currentSkills}">
<table>
<tr><td th:text="${skill.name}"/></tr>
</table>
</div>
<div th:if="${currentSkills != null}">
<table>
<tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr>
</table>
</div>
<div th:if="${currentSkills == null}">
You don't have any skills
</div>
If currentSkills
是一个列表,您可以使用#lists
像这样的实用程序(这比上面的代码更正确,因为它还考虑了对象不为空而是为空的可能性):
<div th:if="!${#lists.isEmpty(currentSkills)}">
<table>
<tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr>
</table>
</div>
<div th:if="${#lists.isEmpty(currentSkills)}">
You don't have any skills
</div>
如果你也可以这样做currentSkills
是一个数组,只需替换#lists
with #arrays
.
请注意,在这两种情况下isEmpty()
无论对象是否为 null 或者有零个项目,都返回 true。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)