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>

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...