gpt4 book ai didi

java - Spring Boot thymeleaf 格式日期给出 "Cannot retrieve hour from null"

转载 作者:行者123 更新时间:2023-12-02 11:39:22 25 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序,我试图在其中以格式化的方式显示出生日期。如果我不使用 thymeleaf 日期格式选项,我能够显示日期,但如果使用格式选项,它会给我“无法对 null 应用格式”

 <td th:text="*{dateOfBirth}">06/23/2013</td>
output: 1990-01-21 00:03:00.0

<td th:text="${#dates.format(dateOfBirth,'dd-mm-yyyy')}">06/23/2013</td>
output ::Cannot apply format on null

我的 pogo 类中的 dateOfBirth 字段声明如下。

@JsonDeserialize(using = CustomeDateDeserializer.class)
@DateTimeFormat(pattern="dd-mm-yyyy")
private Date dateOfBirth;

我尝试过不同的组合,但都给出了相同的错误。任何人都可以帮助我在这里缺少什么吗?

最佳答案

*{dateOfBirth} 具有特定含义。 When you use the * notation ,您引用的是您选择的th:object 的属性。当您使用 ${} 表达式时,您就会失去它。为了指定相同的变量,您应该使用 #object 表达式变量(或命名相关变量的完整路径)。

<td th:text="${#dates.format(#object.dateOfBirth,'dd-MM-yyyy')}">06/23/2013</td>

注意:由于您已经指定了变量的日期格式,我认为 double-bracket syntax应该适合你:

<td th:text="${{#object.dateOfBirth}}">06/23/2013</td>

关于java - Spring Boot thymeleaf 格式日期给出 "Cannot retrieve hour from null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48692048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com