Java Thymeleaf嵌套了each和数组索引
我有一个嵌套的结构,每个结构在树中循环。Html代码如下所示:Java Thymeleaf嵌套了each和数组索引,java,html,spring,thymeleaf,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"&g
<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>
组名
队名
管理员用户是每个用户数组的第0个索引。但是达到它的符号显然是失败的。我可以确认后端数据是正确的。
当此代码在服务器上运行时,我遇到此错误:
org.springframework.web.util.NestedServletException:请求
处理失败;嵌套异常是
org.thymeleaf.exceptions.TemplateProcessingException:异常
评估SpringEL表达式:“team.users[0].score”(teams)
我无法找出这段代码的错误,网络上也没有类似的例子可以解决这个问题。谢谢。如评论中所述:异常的原因是
"org.springframework.expression.spel.SpelEvaluationException: EL1025E:(pos 10): The collection has '0' elements, index '0' is invalid"
如果
team.users
可以有零个元素,只需添加一个th:if条件 根本原因是什么?“异常计算SpringEL表达式”只是最外层的异常。查看最后一个以查看原因。是的,它在底部有第三个原因,上面写着“org.springframework.expression.spel.SpelEvaluationException:EL1025E:(位置10):集合有“0”元素,索引“0”无效“好吧,这是您的问题。这是您的答案;-)然后添加一个th:如果可能发生这种情况,您可以将其作为解决方案编写,我将接受作为答案。谢谢。请发布一个代码片段好吗?