如果 - 其他在Thymeleaf中的每个陈述

use*_*575 5 spring-mvc thymeleaf

我想要的是一个if-else in th:Thymeleaf中的每个陈述.

If currentSkill != null,然后显示包含内容的表格,否则'你没有任何技能'

这是没有if/else的代码:

<div th:each="skill : ${currentSkills}">
    <table>
         <tr><td th:text="${skill.name}"/></tr>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

geo*_*and 15

<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>
Run Code Online (Sandbox Code Playgroud)

如果currentSkills是一个列表,你可以使用#lists像这样的实用程序(这比上面的代码更正确,因为它还考虑了对象不为null但是为空的可能性):

 <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>
Run Code Online (Sandbox Code Playgroud)

你可以这样做,如果currentSkills是一个数组,只需替换#lists#arrays.

请注意,在两种情况下都isEmpty()返回true,无论对象是null还是零项.