2017-10-09 25 views
0

在Freemarker中,我在模型中有一個Map<Map<...>>Freemarker:如何檢查?api-able類型(v 2.3.26)

由於FM故障,查詢第二級Map需要?api。然而,這逃避了正常價值的存在檢查並使事情複雜化。

這是我有:

APINotSupportedTemplateException: The value doesn't support ?api. See requirements in the FreeMarker Manual. 
(FTL type: sequence+extended_hash+string (wrapper: f.c.DefaultToExpression$EmptyStringAndSequence), 
TemplateModel class: f.c.DefaultToExpression$EmptyStringAndSequence, 
ObjectWapper: [email protected](2.3.26, useAdaptersForContainers=true, forceLegacyNonListCollections=true, iterableSupport=trueexposureLevel=1, exposeFields=false, treatDefaultMethodsAsBeanMembers=true, [email protected], ...)) 
The blamed expression: 
==> sortedStatsMap[rowTag.name]! [in template "reports/templates/techReport-boxes.ftl" at line 152, column 84] 

如果我嘗試

sortedStatsMap[rowTag.name]!?is_hash 

那麼這是因爲,如果丟失也失敗了,它給了我:

<#if sortedStatsMap[rowTag.name]?? && sortedStatsMap[rowTag.name]?is_hash> 
    ${mapToJson(sortedStatsMap[rowTag.name]?api.get(boxTag.name))!} 
</#if> 

這與結束據報道,empty_string_and_sequence?is_hash無法應用。 (說錯誤。)

什麼是正確的邏輯來檢查我是否可以使用?api.get(key)?或者正確的方式使用!來處理缺失值或丟失的密鑰?

+0

'?is_hash'應該與'EmptyStringAndSequence'一起使用,或者與其他非''null'一起使用。但我也試過了,它適用於我。你能再檢查一次嗎? – ddekany

+0

'boxTag.name'不總是非''null''字符串嗎?如果是這樣,你遇到了什麼FM故障,這迫使你使用'?api'? – ddekany

+0

確實'thisIsNull!'的結果不支持'?api'。但是我沒有看到你的代碼如何運行,因爲你使用'?api'的地方,你不使用'!'。你確定這個例子是準確的嗎? (我也不明白爲什麼'?is_hash'是相關的。請注意,支持'?api'與做散列無關。) – ddekany

回答

0

您可以檢查一個值是否支持?api?has_api。雖然也許你不需要那個;應該澄清這個例子和與之有關的問題(見我的評論)。