[英]FreeMarker: Check if map value is null
I have a Map whose key and value and custom classes. 我有一个Map,其键和值以及自定义类。 The key class is called Position
and is instantiated with two int (for example new Position(2, 4)
. 键类名为Position
并使用两个int实例化(例如, 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. 我摆脱了这个Position类并将地图转换为SimpleHash
,以便与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. 我现在有一个SimpleHash
其键是一个字符串重新映射位置值(例如"2 4"
),其值为null
或Lot
(自定义)类。
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. 在模板中,我需要检查SimpleMap中给定项的值(作为map
传递)是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>
How to do this? 这个怎么做?
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 . 另见手册的相关部分 。
since freemarker is kind of odd when it comes to null values have two ways to solve it. 因为freemarks有点奇怪,当涉及到null值时,有两种方法可以解决它。 1.Treat it as a missing value : 1.将其作为缺失值:
${map[x + " " + y]!}
do Stuff
${map[x + " " + y]!}
2.Just convert that check into a true/false check. 2.只需将支票转换为真/假支票。 This could be done by using a utility class whith a isNull(Object obj) function. 这可以通过使用带有isNull(Object obj)函数的实用程序类来完成。
utilClass.isNull(map[x + " " + y])==true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.