java map在JSTL EL中的小应用--<c:forEach>遍历Map

准备数据

 1  /**    结构示意图:
 2   类型:  List集合    map对象    LIst集合   Person类对象 String name ; int age
 3 
 4 
 5       mList    ->    map()    ->pList     ->Person p1 坤哥 24
 6                                           ->Person p2 鲲哥 104
 7                              ->pList2
 8                                           ->Person ps1 王小三 24
 9                                           ->Person ps2 王小二 24
10                ->    map2()
11                              ->pList3
12                                           ->Person pr1 张咪咪  19
13                                           ->Person pr2 赵咪咪  21
14                             - >pList4
15                                           ->Person pd1 谢广坤  54
16                                           ->Person pd2 赵四    56  (可能是吧)
17 */

Map所需包 (只限JSTL中)

java.util.HashMap

 

java.util.Map

常 见 Map 指 令 清 单

1. 创建map

Map<String,List<Person>> map = new HashMap<String,List<Person>>();

 

List<Map<String,List<Person>>> mList = new ArrayList<Map<String,List<Person>>>();

其实结构确实不难的。(O - O)```

2. map添加数据

map.put("谢广坤",54)

3. 获取key

 map.keySet()   和   map.entrySet() 

本处用keySet()。因为我菜,用entrySet()遍历的数据不太正常,哪天解决了再写吧。

4. 获取value

 map.values()   注意 ‘ s ’

我 的 主 要 代 码

1. 数据准备

//以谢广坤为例
List<Person> pList4 = new ArrayList<Person>();
Person pd1 = new Person("谢广坤",54);
Person pd2 = new Person("赵四",56);
pList4.add(pd1);pList4.add(pd2);
//。。。
Map<String,List<Person>> map2 = new HashMap<String,List<Person>>();
map2.put("pList4",pList4);
//。。。
List<Map<String,List<Person>>> mList = new ArrayList<Map<String,List<Person>>>();
mList.add(map2);

 

2. 输出所有数据

<c:forEach items="${mList}" var="m" varStatus="id">
        <h2>第${id.count}个map</h2>
    <c:forEach items="${m.keySet()}" var="k">
    <h3>List的名字是:<c:out value="${k}"></c:out></h3>
        <c:forEach items="${m.values()}" var="l">
            <c:forEach items="${l}" var="p">
                <table border="1px dotted blue">
                    <tr>
                        <th>姓名</th>
                        <th>年龄</th>
                    </tr>
                    <tr>
                        <td>${p.name}</td>
                        <td>${p.age}</td>
                    </tr>
                </table>
                <br>
            </c:forEach>
        </c:forEach>
    </c:forEach>
</c:forEach>

 

3. 输出年龄大于50的乡村爱情人物

<c:forEach items="${mList}" var="m">
    <c:forEach items="${m.values()}" var="l">
        <c:forEach items="${l}" var="p">
            <c:if test="${p.age>50}">
                <table border="2px dotted blue">
                <tr>
                    <th>姓名</th>
                    <th>年龄</th>
                </tr>
                <tr>
                    <td>${p.name}</td>
                    <td>${p.age}</td>
                </tr>
            </table>
            </c:if>
        </c:forEach>
    </c:forEach>
</c:forEach>

结 果 样 子

大概就是这个样子

 

 

posted @ 2019-05-17 20:34  foxerz  阅读(2426)  评论(0编辑  收藏  举报