Error: Attempted to call method "format" on null context object
Error: Attempted to call method "format" on null context object
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>
它具有我基于 S.O 的语法。问题 ,
产生错误:
org.springframework.expression.spel.SpelEvaluationException: EL1011E:(pos 11): Method call: Attempted to call method
format(java.time.LocalDate,java.lang.String) on null context object
但是,如果我 运行 这行代码没有 Thymeleaf 格式,它会工作并以默认格式(“2016-05-25”)呈现 table LocalDate 对象。
问题: 为什么我会收到 'null context object' 错误,这是什么意思?我该如何编辑以获得我想要的格式?
要使用 #temporals
对象,您需要在项目中包含 thymeleaf-extras-java8time
模块。 Here 是 extras 模块的 GitHub 页。
This module adds a #temporals
object similar to the #dates
or #calendars
ones in the Standard Dialect, allowing the formatting and creation of temporal objects from Thymeleaf templates.
在 Spring Boot 的 1.4.1 版本中,只需要包含 extras 模块,自动配置会为您设置它。确保您提供了正确的版本,具体取决于您的 Thymeleaf 版本:
- Version 3.0.0.RELEASE - for Thymeleaf 3.0 (requires Thymeleaf 3.0.0+)
- Version 2.1.0.RELEASE - for Thymeleaf 2.1 (requires Thymeleaf 2.1.3+)
我有和你一样的 spring boot 和 thymeleaf 版本,并且收到了同样的错误,只是因为我提供了不合适的 extras (3.0.0) 版本。将它切换到较低版本解决了问题(在我的例子中是在 maven pom 文件中):
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
如果你使用 spring boot 和配置作为代码
添加:templateEngine.addDialect(新的 Java8Time 方言());
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>
它具有我基于 S.O 的语法。问题
org.springframework.expression.spel.SpelEvaluationException: EL1011E:(pos 11): Method call: Attempted to call method format(java.time.LocalDate,java.lang.String) on null context object
但是,如果我 运行 这行代码没有 Thymeleaf 格式,它会工作并以默认格式(“2016-05-25”)呈现 table LocalDate 对象。
问题: 为什么我会收到 'null context object' 错误,这是什么意思?我该如何编辑以获得我想要的格式?
要使用 #temporals
对象,您需要在项目中包含 thymeleaf-extras-java8time
模块。 Here 是 extras 模块的 GitHub 页。
This module adds a
#temporals
object similar to the#dates
or#calendars
ones in the Standard Dialect, allowing the formatting and creation of temporal objects from Thymeleaf templates.
在 Spring Boot 的 1.4.1 版本中,只需要包含 extras 模块,自动配置会为您设置它。确保您提供了正确的版本,具体取决于您的 Thymeleaf 版本:
- Version 3.0.0.RELEASE - for Thymeleaf 3.0 (requires Thymeleaf 3.0.0+)
- Version 2.1.0.RELEASE - for Thymeleaf 2.1 (requires Thymeleaf 2.1.3+)
我有和你一样的 spring boot 和 thymeleaf 版本,并且收到了同样的错误,只是因为我提供了不合适的 extras (3.0.0) 版本。将它切换到较低版本解决了问题(在我的例子中是在 maven pom 文件中):
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
如果你使用 spring boot 和配置作为代码
添加:templateEngine.addDialect(新的 Java8Time 方言());