FreeMarker: check if the map value is null

I have a map whose key and values ​​are custom classes. The key class is called Position and is created using two ints (for example, new Position(2, 4) .

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

In the template, I need to check whether the value of this element in SimpleMap (passed as map ) is either zero or an instance of 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 it?

+4
source share
2 answers

since freemarker is odd when it comes to zero values, there are two ways to solve it. 1. Eliminate this as a missing value:

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

2. Just convert this check to true / false check. This can be done using the utility class with the function isNull (Object obj).

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

Use the operator ?? .

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

See also the related part of the Guide .

+7
source

Source: https://habr.com/ru/post/1387187/


All Articles