检查地图是否包含Freemarker中的特定密钥

时间:2016-11-23 18:32:32

标签: freemarker

在Java中:

Map<String, Object> model = new HashMap<>();
Map<String, String> items = new HashMap<>();
items.put("color", "red");
model.put("items", items);

如果items包含密钥color,我现在想要在我的呈现模板中包含一个代码段。

<#if ???? >
   the map contains a key called color
</#if>

我该怎么替换????与?

1 个答案:

答案 0 :(得分:11)

您可以像这样使用??运算符:

<#if items['color']?? >
   the map contains a key called color
</#if>