错误:尝试在空上下文对象上调用方法"format"

ada*_*een 6 java spring date-formatting thymeleaf spring-boot

Spring-boot v1.4.1
Java v1.8
Thymeleaf v2.1.5.

我视图中的以下代码行:

<td th:each = "sprint : ${sprints}" th:text = "${sprint.releaseDate} ? ${#temporals.format(sprint.releaseDate, 'MMM/dd/yyyy')}"></td>
Run Code Online (Sandbox Code Playgroud)

我有基于SO问题SpringBoot Thymeleaf Ordinal Numbers的语法产生错误:

org.springframework.expression.spel.SpelEvaluationException:EL1011E:(pos 11):方法调用:尝试在空上下文对象上调用方法格式(java.time.LocalDate,java.lang.String)

但是,如果我在没有Thymeleaf格式的情况下运行这行代码,它将工作并以默认格式呈现LocalDate对象表("2016-05-25").

问题:为什么我收到'空上下文对象'错误,这是什么意思?我如何编辑以获得我想要的格式?

Jak*_*Ch. 9

要使用#temporals对象,您需要thymeleaf-extras-java8time在项目中包含模块.是extras模块的GitHub页面.

此模块添加#temporals类似于标准方言中的一个#dates或多个对象#calendars,允许从Thymeleaf模板格式化和创建临时对象.

在Spring Boot的1.4.1版本中,只需要包含extras模块,自动配置将为您设置它.确保您提供的版本正确,取决于您的Thymeleaf版本:

  • 版本3.0.0.RELEASE - 适用于Thymeleaf 3.0(需要Thymeleaf 3.0.0+)
  • 版本2.1.0.RELEASE - 用于Thymeleaf 2.1(需要Thymeleaf 2.1.3+)

我有与你相同的弹簧靴和百万美元版本,并且因为我提供了不合适的附加版本(3.0.0)而收到了同样的错误.将其切换到较低版本修复了问题(在我的情况下在maven pom文件中):

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)


小智 5

如果使用 springboot 和配置作为代码

添加:templateEngine.addDialect(new Java8TimeDialect());