加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:55:05 所属栏目:Java 来源:网络整理
导读: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.问题SpringBoot Thymeleaf
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.问题SpringBoot Thymeleaf Ordinal Numbers,
产生错误:

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格式的情况下运行这行代码,它将工作并以默认格式呈现LocalDate对象表(“2016-05-25”).

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

解决方法

要使用#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+)

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读