2016-11-23 191 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