Thymeleaf嵌套每个和数组索引

时间:2016-11-14 13:25:59

标签: java html spring thymeleaf

我有一个嵌套在树中循环的每个结构。 Html代码如下:

<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>

admin用户是每个用户数组的第0个索引。但达到它的符号显然失败了。我可以确认后端数据是否正确。 当此代码在服务器上运行时,我收到此错误:

  

org.springframework.web.util.NestedServletException:Request   处理失败;嵌套异常是   org.thymeleaf.exceptions.TemplateProcessingException:异常   评估SpringEL表达式:&#34; team.users [0] .score&#34; (队)

我无法弄清楚这段代码有什么问题,网上没有类似的例子可以解决这个问题。感谢。

1 个答案:

答案 0 :(得分:1)

如评论中所述:异常的原因是

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

如果team.users可以包含零元素,只需添加一个:if条件。