gpt4 book ai didi

Thymeleaf - 格式化时检查 NULL 的最佳实践(即日期)

转载 作者:行者123 更新时间:2023-12-03 10:48:50 25 4
gpt4 key购买 nike

我有一个 Thymeleaf 模板代码到 格式化日期 .有时该日期将是 null在返回的对象中。检查 null 的最佳方法是什么?在这种情况下在 Thymeleaf 中?目前模板抛出以下错误:

Caused by: java.lang.IllegalArgumentException: Cannot apply format on null
at org.thymeleaf.util.Validate.notNull(Validate.java:37)
at org.thymeleaf.util.DateUtils.format(DateUtils.java:182)
at org.thymeleaf.expression.Dates.format(Dates.java:164)

最佳答案

您可以使用 thymeleafs 对象实用程序类 Objects或在将对象传递给模板之前对其进行验证。

我更喜欢预验证,因为您通常不想在模板中进行修改。同样,您可以通过这种方式使数据与 View 松散耦合。

关于Thymeleaf - 格式化时检查 NULL 的最佳实践(即日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13825570/

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