2013-02-12 129 views
3

我有一個像下面迭代通過的HashMap中的freemarker

HashMap<String, String> testStatus = new HashMap<String, String>(); 
Map root = new HashMap(); 

public void fmTest() { 

testStatus.put("one","1"); 
testStatus.put("two","2"); 
testStatus.put("three","3"); 
root.put("status", testStatus); 
testStatus.clear(); 
} 

我FTL一個HashMap:

<#list status?keys as key> 
${key} = ${status[key]} 
</#list> 

上面的代碼只是一個例子。我的問題是,如果我循環testStatus hashmap在每個循環中具有不同的值,並在循環結束時清除...我如何讓freemarker顯示來自每個循環的testStatus hashmap的鍵值對。

如果我不清除testStatus hashmap,我會得到累計結果。但是,如果我清除testStatus HashMap中的freemarker無顯示..

+1

當您執行'root.put(「status」,「testStatus」)'它正在維護對同一'HashMap'的引用。它不會創建'testStatus' HashMap的副本。因此,當你清除()時,它的所有內容都被清除了。我認爲要實現你想做的事情,在開始時在你的循環內創建並分配一個'new testStatus HashMap'。 – 2013-02-12 16:09:45

+0

我試過了。它沒有工作。自由標記仍然顯示最後一個循環的散列圖結果。 – 2013-02-12 16:22:58

+2

我認爲你的問題不是freemarker,很明顯,清除testStatus會導致freemarker顯示沒有什麼,但有趣的是,如果你不testStatus顯示累計結果,我認爲你的例子是不夠的,我會問你向我們展示整個方法,循環等...來檢測問題 – 2013-03-11 18:37:40

回答

0
HashMap<String, String> testStatus = new HashMap<String, String>(); 

把這個聲明爲fmTest方法,那麼當你每次調用的方法,新的地圖將投入的根源,所以他們贏了不會累積...