FreeMarker:检查地图值是否为空

发布于 2024-12-21 21:00:28 字数 890 浏览 5 评论 0原文

我有一个 Map,其键和值以及自定义类。关键类称为 Position 并用两个 int 实例化(例如 new Position(2, 4))。

我摆脱了这个 Position 类并将地图转换为SimpleHash 为了将其与 Freemarker 一起使用,我现在有一个 SimpleHash,其键是重新映射位置值的字符串(例如 "2 4")。 ) 及其值是 nullLot (自定义)类

在模板中,我需要检查 SimpleMap 中给定项目的值(作为 map< 传递) 。 /code>) 为 null 或 Lot 实例

        <#list mapMinY..mapMaxY as y>
            <tr>
                <#list mapMinX..mapMaxX as x>
                    <td>
                        <div>
                            <!-- Check if map[x + " " + y] is null -->
                            ${x}, ${y}
                        </div>
                    </td>
                </#list>
            </tr>
        </#list>

如何执行此操作?

I have a Map whose key and value and custom classes. The key class is called Position and is instantiated with two int (for example new Position(2, 4).

I got rid of this Position class and converted the map to a SimpleHash in order to use it with Freemarker. I now have a SimpleHash whose key is a String remapping Position values (for example "2 4") and whose value is either null or a Lot (custom) class.

In the template I need to check if the value of a given item in the SimpleMap (passed as map) is either null or a Lot instance.

        <#list mapMinY..mapMaxY as y>
            <tr>
                <#list mapMinX..mapMaxX as x>
                    <td>
                        <div>
                            <!-- Check if map[x + " " + y] is null -->
                            ${x}, ${y}
                        </div>
                    </td>
                </#list>
            </tr>
        </#list>

How to do this?

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

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

发布评论

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

评论(2

才能让你更想念 2024-12-28 21:00:28

使用 ?? 运算符。

<#if map[x + " " + y]??>It's not null<#else>It's null or missing</#if>

另请参阅手册的相关部分

Use the ?? operator.

<#if map[x + " " + y]??>It's not null<#else>It's null or missing</#if>

See also the related part of the Manual.

独留℉清风醉 2024-12-28 21:00:28

由于 freemarker 在涉及空值时有点奇怪,有两种方法可以解决它。
1.将其视为缺失值:

${map[x + " " + y]!}
   do Stuff
${map[x + " " + y]!}

2.只需将该检查转换为真/假检查。
这可以通过使用带有 isNull(Object obj) 函数的实用程序类来完成。

utilClass.isNull(map[x + " " + y])==true

since freemarker is kind of odd when it comes to null values have two ways to solve it.
1.Treat it as a missing value :

${map[x + " " + y]!}
   do Stuff
${map[x + " " + y]!}

2.Just convert that check into a true/false check.
This could be done by using a utility class whith a isNull(Object obj) function.

utilClass.isNull(map[x + " " + y])==true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文