关于freemarker中循环list中的map问题

来源:互联网 发布:福禄网络 编辑:程序博客网 时间:2024/05/22 13:53

1.从List集合取一个Map集

例如:

Java代码:List list = new ArrayList();
Map map1 = new HashMap();map1.put("phone", "13655555555");map1.put("email", "admin@vip.com");list.add(map1);Map map2 = new HashMap();map2.put("phone", "13888888888");map2.put("email", "china@vip.com");map2.put("address", "beijing");list.add(map2);test.ftl文件:<#list list as map><#list map?keys as itemKey>     <#if itemKey="phone">     Phone:${map[itemKey]}     </#if>     <#if itemKey="email">     Email:${map[itemKey]}     </#if></#list></#list>
2. 复杂的list集合里面map,map里面套有list2集合,list2里面还有map
java代码:
  1. List<Map<String,Object>> typeList=new ArrayList<Map<String,Object>>();
  2. for(MerchantSettledTypeInfo merchantSettledType:merchantSettledTypeSet){
  1. Map<String,Object> typeMap=new HashMap<String,Object>();
  2. ProductTypeInfo type=merchantSettledType.getProductType();
  3. typeMap.put("id", type.getId());
  4. typeMap.put("name", type.getName());
  5. //商品分类list,里面若干个分类map
  6. List<Map<String,String>> categoryList=new ArrayList<Map<String,String>>();
  7. List<ProductCategoryInfo> categoryList =merchantSettledType.getProductCategoryListId());
  8. for( ProductCategoryInfo cate: categoryList){
  9. Map<String,String> categoryMap=new HashMap<String,String>();
  10. categoryMap.put("id", cate.getId().toString());
  11. categoryMap.put("name", cate.getName());
  12. categoryList.add(categoryMap);
  13. }
  14. typeMap.put("categoryList", categoryList);
  15. typeList.add(typeMap);
  • 前台ftl 页面代码:
  •  <#list productTypeCateList as middleMap>               <#list middleMap?keys as itemKey>                <tr>                  <#if itemKey=="name">                    <td>${(middleMap[itemKey])!}</td>                  </#if>                   <#if itemKey=="categoryList">                   <#list middleMap[itemKey] as cateMap>                   <#list cateMap?keys as cateKey>                   <#if itemKey=="name">                   <td>${(cateMap[cateKey])!}</td>                   </#if>                   </#list>                   </#list>                                      </#if>                </tr>               </#list>            </#list>

0 0
原创粉丝点击