我希望在Thymeleaf的th:each语句中使用if-else。
如果currentSkill!= null
,则显示包含内容的表格,否则显示“您没有任何技能”。
这是没有if / else的代码:
<div th:each="skill : ${currentSkills}">
<table>
<tr><td th:text="${skill.name}"/></tr>
</table>
</div>
我希望在Thymeleaf的th:each语句中使用if-else。
如果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>
如果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
替换为#arrays
来执行相同的操作。
请注意,在这两种情况下,如果对象为null或项目数为零,isEmpty()
都会返回true。
您可以使用
<div th:if="${!currentSkills.isEmpty()}">
<table>
<tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr>
</table>
</div>
th:each
中如何按每个项目进行操作呢? - Don Cheadleth:with
轻松地将表达式的结果存储在本地变量中。 - geoand