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

2023-12-21

春季启动 v1.4.1
Java v1.8
百里香叶 v2.1.5。

我认为以下代码行:

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

它的语法是我基于 S.O.问题SpringBoot Thymeleaf 序数 https://stackoverflow.com/questions/36936910/springboot-thymeleaf-ordinal-numbers, 产生错误:

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

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

问题:为什么我收到“空上下文对象”错误,这是什么意思?我怎样才能编辑以获得我想要的格式?


To use #temporals您需要的对象包括thymeleaf-extras-java8time模块到您的项目。Here https://github.com/thymeleaf/thymeleaf-extras-java8time是 extras 模块的 GitHub 页面。

该模块添加了一个#temporals类似于的对象#dates or #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+)

我有与您相同的 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误:尝试在空上下文对象上调用方法“format” 的相关文章

随机推荐