th:each的作用域问题

一个table表格,在第一个td里面通过下标访问有语法提示且运行正常,在第二个td里面访问虽然有语法提示但是运行报错,报错
运行正常代码:
<table width="100%">

    <tr th:each="goods1,goodsIndex:${goods}" th:if="${goodsIndex.index}%2==0">

        <td width="50%">

            <table width="100%"><tr><td><img th:src="${goods[goodsIndex.index].getImg_src()}" /> <img th:src="${goods[goodsIndex.index].getImg_src()}" /></td></td></tr></table>

        </td>

        <td width="50%">

            <table width="100%"><tr><td></td></td></tr></table>

        </td>
        
    </tr>

</table>

运行错误代码:

<table width="100%">
    <tr th:each="goods1,goodsIndex:${goods}" th:if="${goodsIndex.index}%2==0">

        <td width="50%">

            <table width="100%"><tr><td><img th:src="${goods[goodsIndex.index].getImg_src()}" /></td></td></tr></table>

        </td>

        <td width="50%">

            <table width="100%"><tr><td> <img th:src="${goods[goodsIndex.index].getImg_src()}" /></td></td></tr></table>

        </td>

    </tr>

</table>

 

报错信息:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "goods[goodsIndex.index].getImg_src()" (template: "section" - line 27, col 47)

2021-03-02 11:10:59.980 ERROR 1332 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "goods[goodsIndex.index].getImg_src()" (template: "section" - line 27, col 47)] with root cause

th:each放在需要便利的容器的父容器中,然后子容器用其中的数据遍历。

实际用法可参考:https://www.cnblogs.com/SeaWxx/p/10276171.html

仔细对比后发现应该是嵌套的table标签的问题

下面的代码可以正常运行

<td width="50%">

    <img th:src="${goods[goodsIndex.index].getImg_src()}" />
    <img th:src="${goods[goodsIndex.index].getImg_src()}" />
</td>

而要是在两个img周围都加上table标签后,就报错了

<td width="50%">

    <table width="100%"><tr><td><img th:src="${goods[goodsIndex.index].getImg_src()}" /></td></td></tr></table>
    <table width="100%"><tr><td><img th:src="${goods[goodsIndex.index].getImg_src()}" /></td></td></tr></table>

</td>

具体原因是什么还不知道,目前问题未解决

您好,我是问答小助手,你的问题已经有多位小伙伴为您解答了问题,您看下是否解决了您的问题。

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~