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

Check if a map contains a specific key in 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>

我用什么来替换 ????与?

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

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