使用 Spring 框架在 Freemarker 中表示 null

发布于 2024-12-28 15:16:37 字数 259 浏览 5 评论 0原文

我正在开发一个基于 Spring Framerwork 的应用程序。作为一种视图技术,我使用与框架 Freemarker 集成。当存储可视化数据的 java bean 的某些字段为 null 时,就会出现问题。 Freemarker 中没有 null 概念,因此它认为 bean 中根本不存在这些字段。我想问题可以通过自定义类来解决,该类将数据从 java bean 复制到模板中引用的 freemarker 的哈希对象。但我还没有找到 Spring 中哪个类执行此操作。是否有这样的类以及它如何被称为?

I'm developing an application based on Spring Framerwork. As a view technology I use integrated with the framework Freemarker. Problems occur when java bean that stores data for vizualization have a null in some fields. There is no null conception in Freemarker so it considers that there is no these fields in the bean at all. I suppose problem could be solved by customization of class that copies data from the java bean to freemarker's hash object referred in template. But i haven't found what class does it in Spring. Is there such class and how is it called?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七月上 2025-01-04 15:16:37

通常您只需直接在模板中处理空值。例如:

${person.surname!"n/a"}

如果姓氏为空,它将打印“n/a”,或者只是:

${person.surname!}

如果姓氏为空,它将打印出空字符串(什么也没有)。

Usually you just deal with nulls directly in the template. E.g:

${person.surname!"n/a"}

which will print "n/a" in case of a null surname, or just:

${person.surname!}

which will print out the empty string (nothing) in case of a null surname.

童话 2025-01-04 15:16:37

您可以使用“!”操作员。这是一个示例:

${your_property!""}

如果 your_property 为 null,它将打印空字符串“”。

You can use the "!" operator. Here is an example :

${your_property!""}

It will print the empty string "" if your_property is null.

你的背包 2025-01-04 15:16:37

如果属性链中的任何部分都可以为空,您还可以在其周围放置括号以防止任何部分为空。例如,如果你 pu

${person.car.door.color!"<no value"}

你只防止颜色为空。但如果也可能发生门、车或整个人失踪的情况,你就必须把

${(person.car.door.color)!"<no value"}

If any part in your property chain can be null, you can also put parantheses around it to guard against any part being null. E.g. if you pu

${person.car.door.color!"<no value"}

you only guard against the color being null. But if it could also happen that the door, the car or the whole person is gone missing, you have to put

${(person.car.door.color)!"<no value"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文