Freemarker 迭代地图列表中的哈希图

Freemarker iterating over hashmap in list of map

看过类似的问题,但还是没找到很好的解决方法

这个节目:

List<HashMap<String, String>> list = ArrayList<HashMap<String, String>>();
String abc = "a,b,c";

for(String x : abc.split(",")){
  // how to get this ?
  HashMap<String, String> map = new HashMap<String, String>();
  map.put("one", x);
  map.put("two", x);
  map.put("three",x);
  list.add(map);
}

HashMap<String, Object> root = new HashMap<String, Object>();
root.put("abc",list);
root.put("test", "value");

如果我想使用freemark程序从根地图迭代地图,怎么办?

如果您将代码放入方法中的模型 bean

  public HashMap<String, Object> getRoot() {
    // ... your code
    return root;
  }

然后您可以像这样访问 abc 键下的地图列表:

[
<#list root['abc'] as map>
  {
  <#list map?keys as key>
    ${key}: ${map[key]}
  </#list>
  }
</#list>
]

代替root['abc'],你也可以使用root.abc,但第一个版本强调abc是一个散列映射键,而不是属性的root.

这是结果输出:

[
  {
    two: a
    one: a
    three: a
  }
  {
    two: b
    one: b
    three: b
  }
  {
    two: c
    one: c
    three: c
  }
]