freemaker中遍历List<Map<String,Map>>
freemaker中遍历List<Map<String,Map>>
[{ java所定义的版本中不包括() =[{1=java2 EE}, {2=java2 ME}, {3=java2 SE}, {4=java2 HE}]}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ftl文件:
List<Map<String, Map<String, String>>>
<#if (datas)??>
<#-- 遍历list -->
<#list datas as data >
序号:${data_index}
<#if (data)??>
<#-- 遍历第一层MAP -->
<#list data?keys as outkey>
<#assign outMap = data[outkey] />
outkey:${outkey}
<#if outMap??>
<#list outMap?keys as innerkey>
innerkey:${innerkey}--> ${outMap[innerkey]}
</#list>
</#if>
</#list>
</#if>
</#list>
</#if>
java 代码:
public static void main(String[] args) throws Exception {
File file=new File("/Users/gefangyu/t2/main.ftl");
String str = "";
try {
FileInputStream in=new FileInputStream(file);
// size 为字串的长度 ,这里一次性读完
int size=in.available();
byte[] buffer=new byte[size];
in.read(buffer);
in.close();
str =new String(buffer,"UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
List<Map<String, Map<String, String>>> list = new ArrayList<Map<String,Map<String,String>>>();
Map<String, Map<String, String>> map1 = new HashMap<String, Map<String,String>>();
Map<String, Map<String, String>> map2 = new HashMap<String, Map<String,String>>();
Map<String, String> innerMap1 = new HashMap<String, String>();
Map<String, String> innerMap2 = new HashMap<String, String>();
Map<String, String> innerMap3 = new HashMap<String, String>();
Map<String, String> innerMap4 = new HashMap<String, String>();
innerMap1.put("key1", "value1");
innerMap1.put("key2", "value2");
innerMap2.put("key3", "value3");
innerMap2.put("key4", "value4");
innerMap3.put("key5", "value5");
innerMap3.put("key6", "value6");
innerMap4.put("key7", "value7");
innerMap4.put("key8", "value8");
map1.put("innerMap1", innerMap1);
map1.put("innerMap2", innerMap2);
map1.put("innerMap3", innerMap3);
map2.put("innerMap4", innerMap4);
list.add(map1);
list.add(map2);
Map<String, Object> param = new HashMap<String, Object>();
param.put("datas", list);
FreemarkerFactory.getInstance().createFreemarkerProcessor().process(str, param, "/Users/gefangyu/t2/", "temp.shtml");
}
运行结果: