如何将此模板片段从Thymeleaf 2.1.4迁移到Thymeleaf 3.0?

时间:2016-05-16 09:20:35

标签: thymeleaf

我有一个这样的模板:

sequence

它与Thymeleaf 2.1.4一起使用但是3.0.0会引发以下异常:

/* for Swift 3.1 */
// ... as above
for f in sequence(first: first, next: { $0 + interval })
    .prefix(while: { $0 <= last }) {
    print(f)
    n += 1
} // 0 2 4 6 8 10
print(n) // 6

知道如何解决这个问题吗?

注意: 这不是循环,因此没有{/ 1}}对象,如

<table th:with="isEven=false"> <tr th:if="*{foo} != null" class='odd' th:class="${isEven=!isEven}?'even':'odd'"> foo </tr> <tr th:if="*{bar} != null" class='odd' th:class="${isEven=!isEven}?'even':'odd'"> bar </tr> </table>

根据org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "isEven=!isEven" (template: "foo" - line 10, col 34) Caused by: java.lang.UnsupportedOperationException: Cannot set values into VariablesMap instances from OGNL Expressions 条件生成varStat,并且我希望为它们进行偶数/奇数切换。

修改:我为此问题打开了Github issue

1 个答案:

答案 0 :(得分:1)

Thymeleaf 3不允许将上下文变量修改为OGNL / SpringEL表达式的横向效应(实际上这是2.1中错误允许的内容)。

但如果我正确理解您的代码,您应该能够在th:with中创建所需的标记。像这样:

<table th:with="hasFoo=(*{foo} != null)">
    <tr th:if="${hasFoo}" class='even'>foo</tr>
    <tr th:if="*{bar} != null" class='odd' th:class="${hasFoo}? even : odd">bar</tr>
</table>
相关问题