Thymeleaf中使用#numbers.sequence结合变量报错问题求助
解决Thymeleaf中#numbers.sequence的SpEL解析错误
问题出在你在SpEL表达式内部嵌套了${}语法,Thymeleaf不允许这种写法。要修复这个问题,需要先把动态变量的值正确提取出来,再传递给#numbers.sequence。
修复步骤:
在
th:with中新增变量存储动态计数
利用Thymeleaf的#vars.get()方法获取动态命名的变量值,避免嵌套${}。修改外层的th:block,添加imageCount变量:<th:block th:with="iBladeIndex=|${iterstat.index}|, imageCount=${#vars.get('Blade__' + iBladeIndex + '__CutterMultiImagesCount')}">使用提取后的变量生成序列
直接用imageCount作为#numbers.sequence的参数,不再嵌套表达式:<tr th:each="i: ${#numbers.sequence(0, imageCount - 1)}"> <td th:text="${i}"></td> </tr>
完整代码示例:
<th:block th:with="iBladeIndex=|${iterstat.index}|, imageCount=${#vars.get('Blade__' + iBladeIndex + '__CutterMultiImagesCount')}"> <!-- 原正确显示计数的代码可保留 --> <tr> <td th:text="${imageCount}"></td> </tr> <!-- 修复后的循环代码 --> <tr th:each="i: ${#numbers.sequence(0, imageCount - 1)}"> <td th:text="${i}"></td> </tr> </th:block>
错误原因说明:
你原来的写法${#numbers.sequence(0, ${Blade__${iBladeIndex}__CutterMultiImagesCount} - 1)}中,${}内部的整个内容是一个SpEL表达式,不能再嵌套另一个${}。#vars.get()方法可以通过字符串拼接的变量名直接获取对应的值,完美适配这种动态命名变量的场景。
内容的提问来源于stack exchange,提问作者anjanesh
相关产品推荐
相关产品推荐

