map、list、map(Object,map)的前臺獲取遍歷

 

 

一、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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章