You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot与Thymeleaf嵌套th:block报错,求替代方案

问题解决:Thymeleaf嵌套th:block报错的真实原因及修复

首先明确:Thymeleaf完全支持嵌套th:block,你遇到的报错不是因为嵌套导致的,而是代码里的HTML语法错误——看这段代码:

<td th:rowspan="${record.size()} th:if="${iterStat.first}">Attribute : </td>

这里的th:rowspan属性没有闭合引号,应该写成th:rowspan="${record.size()}",缺失的闭合引号让Thymeleaf解析器混乱,才抛出了错误提示。

修正后的完整代码

<th:block th:each="claim:${claimEditor}">
    <table>
        <tr>
            <td>Number of claim : </td>  
            <td th:text="${claim.numberOfClaim}"></td>
        </tr>

        <th:block th:unless="${#lists.isEmpty(claim.record)}">
            <th:block th:each="record, iterStat:${claim.record}">
                <tr>
                    <td th:rowspan="${record.size()}" th:if="${iterStat.first}">Attribute : </td>  
                    <td th:text="${record}"></td>
                </tr>
            </th:block>
        </th:block>
    </table>
</th:block>

可选简化方案(非必须,仅优化层级)

如果想减少嵌套层级,也可以把th:unless和th:each合并到同一个th:block上,代码会更简洁:

<th:block th:each="claim:${claimEditor}">
    <table>
        <tr>
            <td>Number of claim : </td>  
            <td th:text="${claim.numberOfClaim}"></td>
        </tr>

        <th:block th:unless="${#lists.isEmpty(claim.record)}" th:each="record, iterStat:${claim.record}">
            <tr>
                <td th:rowspan="${record.size()}" th:if="${iterStat.first}">Attribute : </td>  
                <td th:text="${record}"></td>
            </tr>
        </th:block>
    </table>
</th:block>

内容的提问来源于stack exchange,提问作者robert trudel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 16:57:51