FreeMarker:检查地图值是否为空
我有一个 Map,其键和值以及自定义类。关键类称为 Position
并用两个 int 实例化(例如 new Position(2, 4)
)。
我摆脱了这个 Position 类并将地图转换为SimpleHash
为了将其与 Freemarker 一起使用,我现在有一个 SimpleHash
,其键是重新映射位置值的字符串(例如 "2 4"
)。 ) 及其值是 null
或 Lot
(自定义)类
在模板中,我需要检查 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
??
运算符。另请参阅手册的相关部分。
Use the
??
operator.See also the related part of the Manual.
由于 freemarker 在涉及空值时有点奇怪,有两种方法可以解决它。
1.将其视为缺失值:
2.只需将该检查转换为真/假检查。
这可以通过使用带有 isNull(Object obj) 函数的实用程序类来完成。
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 :
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.