2016-11-23 192 views
2

在Java中的特定键:检查如果地图包含的Freemarker

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> 

我怎么更换????用?

回答

6

您可以使用??操作是这样的:

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

或者,如果钥匙确实是一个常数,那么就'items.color ??'。 – ddekany