美文网首页
freemarker 从List集合取一个Map集

freemarker 从List集合取一个Map集

作者: 夜空最亮的9星 | 来源:发表于2018-08-31 21:15 被阅读18次

    从List集合取一个Map集

    例如:

        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>
    

    复杂的list集合里面map,map里面套有list2集合,list2里面还有map

        List<Map<String,Object>> typeList=new ArrayList<Map<String,Object>>();
        for(MerchantSettledTypeInfo merchantSettledType:merchantSettledTypeSet){
    
            Map<String,Object> typeMap=new HashMap<String,Object>();
            ProductTypeInfo type=merchantSettledType.getProductType();
            typeMap.put("id", type.getId());
            typeMap.put("name", type.getName());
            //商品分类list,里面若干个分类map
            List<Map<String,String>> categoryList=new ArrayList<Map<String,String>>();
            List<ProductCategoryInfo> categoryList =merchantSettledType.getProductCategoryListId());
            for( ProductCategoryInfo cate: categoryList){
                Map<String,String> categoryMap=new HashMap<String,String>();
                categoryMap.put("id", cate.getId().toString());
                categoryMap.put("name", cate.getName());
                categoryList.add(categoryMap);
            }
            typeMap.put("categoryList", categoryList);
            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>
    

    demo3

    原文链接

    image
    <#list keyan as middleMap>
        <#list middleMap?keys as itemKey>
            <#assign keys=middleMap?keys/>
            <#list keys as key>
                key:${key}
                <#if itemKey=="${key}">
                    <#list middleMap[itemKey] as cateMap>
                        数字:${cateMap.rid!}
                    </#list>
                </#if>
            </#list>
        </#list>
    </#list>
    

    相关文章

      网友评论

          本文标题:freemarker 从List集合取一个Map集

          本文链接:https://www.haomeiwen.com/subject/nkiewftx.html