微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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>

原文地址:https://www.jb51.cc/java/120882.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐