gpt4 book ai didi

java - 在 freemarker 中遍历 hashmap

转载 作者:行者123 更新时间:2023-12-03 18:36:36 29 4
gpt4 key购买 nike

我有一个像下面这样的 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();
}

我的 FTL:

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

以上代码只是一个例子。我的问题是,如果我循环 testStatus HashMap 以在每个循环中具有不同的值并在循环结束时清除...我如何让 freemarker 显示每个循环中的 testStatus HashMap 的键值对。

如果我不清除 testStatus HashMap ,我会得到累积结果。但是,如果我清除 testStatus HashMap ,freemarker 将不显示任何内容。

最佳答案

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

把这个声明放到fmTest方法中,那么每次调用这个方法的时候,都会在root中放入一个新的Map,这样就不会累加了...

关于java - 在 freemarker 中遍历 hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836417/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com