freemarker模板数据解析1--页面语法直接获取
对后端接口传递过来的Map<String,List
对于普通Map<String,String>数据,我们很清楚,直接上代码:
·接口提供数据:
Map<String,String> map = new HashMap<>();
map.put("k1","v1");
map.put("k2","v2");
map.put("k3","v3");
request.setAttribute("map",map);//返回页面
·页面代码:
<#if map??>
<#if map?size=0>
没有数据
<#else>
<#list map?keys as m>
${m}=${map[m]}<br>
</#list>
</#if>
</#if>
·效果:
图片可能加载不出来,大概是这个效果:(hashMap有无序性)
k3=v3
k1=v1
k2=v2
对于多层嵌套Map<String,List<Map<String,String>>>的来说,如下:
·接口数据:
List<Map> mapList = new ArrayList<>();
Map map1 = new HashMap<>();
map1.put("a1","1");
map1.put("a2","11");
mapList.add(map1);
Map map2 = new HashMap<>();
map2.put("a1","2");
map2.put("a2","22");
mapList.add(map2);
Map map3 = new HashMap<>();
map3.put("a1","3");
map3.put("a2","33");
mapList.add(map3);
HashMap<String,List<Map>> hashMap = new HashMap();
hashMap.put("example",mapList);
request.setAttribute("hashMap",hashMap);//hashMap:{example=[{a1=1, a2=11}, {a1=2, a2=22}, {a1=3, a2=33}]}
·页面代码:
<#if hashMap??>
<#if hashMap?size=0>
没有数据
<#else>
<#list hashMap?keys as ml>
<#assign item = hashMap[ml]>
<#list item as map>
${map.a1!}-${map.a2}<br/>
</#list>
</#list>
</#if>
</#if>
·效果:
图片可能加载不出来,大概是这个效果:(hashMap有无序性)
1-11
2-22
3-33
嵌套太多层脑子着实不够用,多写点注释帮助记忆。