2014-02-28 13 views
0

我試圖通過HashMap<SeapSubscription, List<PiNotice>>在freemarker模板中進行迭代。 該地圖不包含任何空值(按鍵或值)。迭代對象的映射:Freemarker中的列表

在Freemarker的代碼是:

<#list subscriptionsWithPiNotices?keys as s> 
${s.title} 

    <#list subscriptionsWithPiNotices[s] as piNotice> 
    Autoritate contractanta: ${piNotice.contractingAuthorityName} 
    . 
    . 
    </#list> 
</#list> 

如果我從第二列表(< #list subscriptionsWithPiNotices [秒]作爲piNotice>)所有的工作除去迭代(即遍歷映射鍵,但是當我添加第二個部分,試圖遍歷地圖的價值,我得到一個空/缺失異常

      FreeMarker template error: The following has evaluated to null or missing: 

==> subscriptionsWithPiNotices [S] [模板「SEAP訂閱快訊.ftl」在第21行,第16欄第]

提示:如果發生故障的表達已知是合法空/缺少, 要麼指定與myOptionalVar myDefault的默認值,或使用 <的#if myOptionalVar ??>時,本! < #else>失蹤時。 (這些 僅覆蓋表達的最後步驟;以覆蓋整個 表達,使用parenthessis:(myOptionVar.foo)myDefault, (myOptionVar.foo)??

的失敗指令(FTL堆棧跟蹤)! :

==> #list subscriptionsWithPiNotices [s] a ... [在模板「seap-subscription-newsletter.ftl」第21行,第9列] #list subscriptionsWithPiNotices?keys ... [in template「 seap-subscription-newsletter.ftl「在第18行第5列]

我再說一遍, hMap,它只有一個關鍵字,其中一個ArrayList裏面有一個項目。所以沒有理由報告一個空,是嗎?

+0

這是FTL的年齡問題,它沒有'Map'類型。它有一個「散列」類型,但只支持「字符串」鍵(用於子變量)。然後'BeansWrapper'靜靜地將'toString()'s's'作爲'[]'需要一個字符串,然後它當然不會找到'Map'條目......你會發現一些關於可能的SO答案解決方法。 – ddekany

+0

密鑰的類型是什麼? –

回答

0

最後我設法解決這個問題,通過配置

BeansWrapper bw = BeansWrapper.getDefaultInstance(); 
bw.setSimpleMapWrapper(true); 
cfg.setObjectWrapper(bw); 

,然後使用

地圖(key)的語法,而不是圖[鍵]或map.key。它似乎適用於任何類型的密鑰類。

+0

這個問題是,你使用純粹的'BeansWrapper',它有自己的疣,也不會在使用'DefaultObjectWrapper'(推薦)的設置上工作。從2.3.22開始,你可以使用更好的解決方案''api'。請參閱[本答案](http://stackoverflow.com/a/28794648/606679)或[本FAQ條目](http://freemarker.org/docs/app_faq.html#faq_nonstring_keys)。 – ddekany