If-Else in th: every statement in Thymeleaf

What I want is an if-else in th: every statement in Thymeleaf.

If currentSkill != null, then show the table with the contents, otherwise "You have no skills"

This is the code without if / else:

<div th:each="skill : ${currentSkills}">
    <table>
         <tr><td th:text="${skill.name}"/></tr>
    </table>
</div>
+4
source share
2 answers
<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 currentSkillsthis is a list, you can use a utility #listslike this (which is more correct than the code above, since it also takes into account the possibility when the object is not null but 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 can do the same if currentSkills- this is an array by simply replacing #listswith #arrays.

, isEmpty() true, .

+12

<div  th:if="${!currentSkills.isEmpty()}">
    <table>
         <tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr>
    </table>
</div>
0

Source: https://habr.com/ru/post/1541119/


All Articles