【问题标题】:Thymeleaf nested each and array indexesThymeleaf 嵌套每个和数组索引
【发布时间】:2016-11-14 13:25:59
【问题描述】:

我有一个嵌套的每个结构,它遍历一棵树。 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: 请求 处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:异常 评估 SpringEL 表达式:“team.users[0].score”(团队)

我不知道这段代码有什么问题,网上也没有类似的例子可以解决这个问题。谢谢。

【问题讨论】:

  • 根本原因是什么? “异常评估 SpringEL 表达式”只是最外层的异常。看最后一个就知道原因了。
  • 是的,它在底部有第三个由'无效”
  • 好吧,这就是你的问题。
  • 那么这就是你的答案 ;-) 然后添加一个 th:if 如果这种情况可能发生
  • 你可以把它写成一个解决方案,我会接受作为答案谢谢。

标签: java html spring thymeleaf


【解决方案1】:

在cmets中提到:异常的原因是

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    相关资源
    最近更新 更多