Error: attempt to call the format method on a null context object

Spring-boot v1.4.1
Java v1.8
Thimeleaf v2.1.5.

The following line of code in my opinion:

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

which has the syntax that I base on the SO question Timmelaf SpringBoot serial numbers , gives an error:

org.springframework.expression.spel.SpelEvaluationException: EL1011E: (item 11): method call: attempt to call the format (java.time.LocalDate, java.lang.String) method in a null context object

However, if I run this line of code without Thymeleaf formatting, it works and displays the LocalDate object table in the default format ("2016-05-25").

Question: Why do I get the error "null context object" and what does it mean? And how can I edit to get the formatting I need?

+6
source share
1 answer

To use the #temporals object, you need to include the thymeleaf-extras-java8time project in the project. Here is the GitHub page of the module for additional functions.

This module adds a #temporals object, similar to the #dates or #calendars tags in a standard dialog box, allowing you to format and create temporary objects from Timeleaf templates.

In version 1.4.1 of Spring Boot, you only need to enable the extras module, and autoconfiguration will configure it for you. Make sure you provide the correct version, depending on your version of Thymeleaf:

  • Version 3.0.0.RELEASE - for Thymeleaf 3.0 (Thymeleaf 3.0.0 + required)
  • Version 2.1.0.RELEASE - for Thymeleaf 2.1 (Thymeleaf 2.1.3 + required)

I have the same Spring boot and thymeleaf versions as you, and I got the same error only because I am providing an unacceptable version of additional functions (3.0.0). Switching to a lower version fixed the problem (in my case in the maven pom file):

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

Source: https://habr.com/ru/post/1011518/


All Articles