Thymeleaf表达式[#temporals.format$ {event.eventStart},'pattern'] @ 20:EL1043E:意外令牌预期为“ rparen”,但为“ lcurly{”

问题描述

event.eventStart是OffsetDateTime对象。

添加到POM

<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
            <version>3.0.4.RELEASE</version>
</dependency>

将Java8TimeDialect添加到TemplateEngine

 final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addDialect(new Java8TimeDialect());

预期-此行在我的电子邮件显示格式化时间。

<span th:text="${#temporals.format( ${event.eventStart},'yyyy-MM-dd HH:mm:ss ZZZZ')}"></span>

实际-

Expression [#temporals.format( ${event.eventStart},'yyyy-MM-dd HH:mm:ss ZZZZ')] @20: EL1043E: Unexpected token. Expected 'rparen())' but was 'lcurly({)'

解决方法

您不能嵌套${...}表达式。只需删除中间的多余部分即可。例如:

th:text="${#temporals.format(event.eventStart,'yyyy-MM-dd HH:mm:ss ZZZZ')}"