一、map
后台代码:
Map<String,String> map2 = new HashMap();
map2.put("a","hello world");
map2.put("b","this is map");
request.setAttribute("map2",map2);
前台代码:
<c:forEach var="item" items="${map2}">
${item.key} > 或者 ${item['a']}
${item.value} <br>
</c:forEach>
二、map(Object,map)
后台代码:
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
List<String> list2 = new ArrayList<String>();
list2.add("aaaaaa");
list2.add("bbbbbb");
Map<String,List<String>> map = new HashMap();//(List<Map(k,v)>同理)
map.put("a",list);
map.put("b",list2);
request.setAttribute("map",map);
前台代码:
<c:forEach var="item" items="${map['a']}">
${item }<br>
</c:forEach><br>
<c:forEach var="item" items="${map['b']}">
${item }<br>
</c:forEach> <br>
// map中值为列表,直接遍历列表中的每一项<br>
<c:forEach var="item" items="${map}">
<c:forEach items="${item.value}" var="it">
${it }<br>
</c:forEach>
</c:forEach>
三、list
后台代码:
List list=new ArrayList();
list.add(user1);
list.add(user2);
list.add(user3);
request.setAttribute(“list”,list);
user是一个类,里面有set/get方法
前台代码:
<c:forEach var="user" items="${list}">
<c:out value="${user.username}" />
</c:forEach>
本文深入探讨了Java集合框架中的Map和List使用方法,包括Map的基本操作、Map与List的结合使用,以及List的遍历技巧。通过具体示例,讲解了如何在后台设置Map和List数据,并在前端通过JSP和EL表达式进行数据展示。
677

被折叠的 条评论
为什么被折叠?



