gpt4 book ai didi

java - Thymeleaf 嵌套each 和数组索引

转载 作者:行者123 更新时间:2023-11-30 07:02:53 26 4
gpt4 key购买 nike

我有一个嵌套的each 结构,它循环穿过一棵树。 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>

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

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "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 条件即可。

关于java - Thymeleaf 嵌套each 和数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40589851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com