Thymeleaf 中的 If-Else:each 语句 [英] If-Else in a th:each statement in Thymeleaf
问题描述
我想要的是 Thymeleaf 中 th:each 语句中的 if-else.
如果currentSkill != null
,则显示包含内容的表格,否则'您没有任何技能'
这是没有 if/else 的代码:
<表格><tr><td th:text="${skill.name}"/></tr>
<表格><tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr><div th:if="${currentSkills == null}">你没有任何技能
如果 currentSkills
是一个列表,您可以像这样使用 #lists
实用程序(这比上面的代码更正确,因为它还考虑了以下可能性对象不为空而是为空):
<表格><tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr><div th:if="${#lists.isEmpty(currentSkills)}">你没有任何技能
如果 currentSkills
是一个数组,你也可以这样做,只需将 #lists
替换为 #arrays
.
请注意,在这两种情况下,isEmpty()
都会返回 true,无论对象为 null 还是具有零个项目.
What I want is an if-else in a th:each statement in Thymeleaf.
If currentSkill != null
, then show table with content, else 'You don't have any skills'
This is the code without the 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
is a list, you can use the #lists
utility like so (which is more correct than the above code since it also takes into account the possibility where the object is not null but is empty):
<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>
You could do the same if currentSkills
is an array by just replacing #lists
with #arrays
.
Note that in both cases isEmpty()
returns true whether the object is null or has zero items.
这篇关于Thymeleaf 中的 If-Else:each 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!