Java 在freemarker中迭代hashmap

Java 在freemarker中迭代hashmap,java,freemarker,Java,Freemarker,我有一个如下所示的hashmap 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",

我有一个如下所示的hashmap

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();
}
HashMap testStatus=newhashmap();
Map root=newhashmap();
公共测试(){
testStatus.put(“一”、“一”);
testStatus.put(“两个”,“2”);
testStatus.put(“三”、“三”);
root.put(“status”,testStatus);
testStatus.clear();
}
我的FTL:

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

${key}=${status[key]}
上面的代码只是一个示例。我的问题是,如果我循环testStatus hashmap,使每个循环中有不同的值,并在循环结束时清除。。。如何让freemarker显示每个循环中testStatus hashmap的键值对

如果我没有清除testStatus哈希映射,我会得到累积的结果。但是如果我清除testStatus hashmap,freemarker将不显示任何内容。

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

将此声明放入fmTest方法中,然后每次调用该方法时,都会将一个新映射放入根中,这样它们就不会被累积…

当您执行
root.put(“status”,“testStatus”)
时,它会维护对相同
HashMap
的引用。它没有创建
testStatus
HashMap的副本。因此,当您
clear()
it时,它的所有内容都会被清除。我认为要实现您想要做的事情,首先在循环中创建并分配一个
新的testStatus HashMap
,我试过了。它不起作用。Free marker仍然显示上次循环的hashmap结果。我认为您的问题不在于freemarker,很明显,清除testStatus将导致freemarker显示不显示任何内容,但有趣的是,如果testStatus不显示累计结果,我认为您的示例不充分,我会要求您向我们展示整个方法、循环等。。。探测problem@user1629109:您的代码示例不足以让我们猜测发生了什么。请发布真正的代码:“上面的代码只是一个示例。我的问题是,如果我循环testStatus hashmap,使其在每个循环中具有不同的值,并在循环结束时清除……如何让freemarker显示每个循环中testStatus hashmap的键值对。”