Barış Ürüm Barış Ürüm - 18 days ago 11
HTML Question

Thymeleaf nested each and array indexes

I have a nested each structure that loops through a tree. Html code is as follows:

<div th:each="group : ${groups}" class="row corp-teams-group-row">
<h2 th:text="'- ' + ${group.name}">GROUP NAME</h2>
<div th:each="team : ${group.teams}" class="row corp-teams-teams-row">
<h2 th:text="'- ' + ${team.name}">TEAM NAME</h2>
<div class="col-xs-6 corp-man-wrapper corp-admin-wrapper">
<div class="corp-user-image-container">
<div class="corp-user-image-wrapper">
<img th:src="${team.users[0].image}">
</div>
<div class="corp-bugcount"><span th:text="${team.users[0].score}"></span></div>
</div>
<div class="corp-user-data-container">
<h2 th:text="${team.users[0].name} + ' ' + ${team.users[0].surname}"></h2>
<div class="corp-user-email" th:text="${team.users[0].email}"></div>
</div>
</div>
<div class="col-xs-6 corp-man-wrapper">
<h3 th:text="${team.newFeatureCount}"></h3>
<h3 th:text="${team.defectCount}"></h3>
</div>
<div th:each="user : ${team.users}" class="col-xs-6 corp-man-wrapper">
<div class="corp-user-image-container">
<div class="corp-user-image-wrapper">
<img th:src="${user.image}">
</div>
<div class="corp-bugcount"><span th:text="${user.score}"></span></div>
</div>
<div class="corp-user-data-container">
<h2 th:text="${user.name} + ' ' + ${user.surname}"></h2>
<div class="corp-user-email" th:text="${user.email}"></div>
</div>
</div>
</div>
</div>


The admin user is 0th index of each user array. But the notation to reach it obviously fails. I can confirm that the back-end data is correct.
I get this error when this code runs on the server:


org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
org.thymeleaf.exceptions.TemplateProcessingException: Exception
evaluating SpringEL expression: "team.users[0].score" (teams)


I can't figure out what is wrong with this code and no similar examples on the net that cracks this problem. Thanks.

Answer

As mentioned in the comments: the cause of the exception is

"org.springframework.expression.spel.SpelEvaluationException‌​: EL1025E:(pos 10): The collection has '0' elements, index '0' is invalid"

Simply add an th:if condition if the team.users can have zero elements.

Comments