再講<c:forEach>之前,現講一下讓EL表達式生效的語句
<% @ page isELIgnored="false"%>這句語句在你想讓EL表達式生效的情況下,必須要加載jsp中。
<c:forEach>中各個常用屬性的解釋如下:
items:要遍歷的集合,通常用EL表達式表示:pageContext.setAttribute("mylist",list); ${mylist}
var:變量
varStatus:變量的狀態 varStatus的屬性有index,和count.其中index從0開始數起,count:個數。
下面的代碼講了java中常用的集合:
1 <%@ page import="java.util.ArrayList" %> 2 <%@ page import="com.supwisdom.domain.Student" %> 3 <%@ page import="java.util.HashMap" %> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <%--el表達式生效的語句--%> 7 <%@page isELIgnored="false" %> 8 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 9 <%--防止調用函數時報錯--%> 10 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 11 <html> 12 <body> 13 <h2>Hello World!</h2> 14 <%--forEach的用法--%> 15 <%--用forEach遍歷list集合--%> 16 <% 17 ArrayList<String> strings = new ArrayList<String>(); 18 strings.add("王昭君"); 19 strings.add("西施"); 20 strings.add("霍去病"); 21 strings.add("楊開慧"); 22 pageContext.setAttribute("ouyangfeng",strings); 23 %> 24 <c:forEach var="item" items="${ouyangfeng}" varStatus="status"> 25 <c:out value="${item}"></c:out> 26 status.index: <c:out value="${status.index}"></c:out> 27 status.count:<c:out value="${status.count}"></c:out><br> 28 </c:forEach> 29 <%--遍歷list對象的集合--%> 30 <% 31 Student student = new Student(); 32 student.setId(1); 33 student.setName("zyh"); 34 student.setAge(16); 35 Student student2 = new Student(); 36 student2.setId(2); 37 student2.setName("ouyangfeng"); 38 student2.setAge(29); 39 ArrayList<Student> students = new ArrayList<Student>(); 40 students.add(student); 41 students.add(student2); 42 pageContext.setAttribute("plump",students); 43 %> 44 45 <c:forEach var="item" items="${plump}"> 46 學生的姓名: <c:out value="${item.name}"></c:out> 47 學生的年齡: <c:out value="${item.age}"></c:out> 48 學生的身份證號:<c:out value="${item.id}"></c:out> 49 <br> 50 </c:forEach> 51 <%--遍歷list數組集合--%> 52 <% 53 ArrayList<String[]> sixi = new ArrayList<String[]>(); 54 String[] str= new String[]{"country","china"}; 55 String[] str2=new String[]{"address","NewYork"}; 56 String[] str3= new String[]{"student","pupil"}; 57 sixi.add(str); 58 sixi.add(str2); 59 sixi.add(str3); 60 pageContext.setAttribute("array",sixi); 61 %> 62 <c:forEach var="item" items="${array}"> 63 <c:out value="${item[0]}"></c:out> <%--數組中的第一個元素--%> 64 <c:out value="${item[1]}"></c:out> <%--數組中的第二個元素--%> 65 <br> 66 </c:forEach> 67 <%--遍歷map集合--%> 68 <% 69 HashMap<String, String> map = new HashMap<String, String>(); 70 map.put("China","Reservation"); 71 map.put("France","Romantic"); 72 map.put("America","Innovation"); 73 pageContext.setAttribute("hashmap",map); 74 %> 75 <c:forEach var="item" items="${hashmap}"> 76 鍵是:<c:out value="${item.key}"></c:out> 77 值是:<c:out value="${item.value}"></c:out><br> 78 </c:forEach> 79 </body> 80 </html>
tomcat運行的結果為:
1 Hello World! 2 3 王昭君 status.index: 0 status.count:1 4 西施 status.index: 1 status.count:2 5 霍去病 status.index: 2 status.count:3 6 楊開慧 status.index: 3 status.count:4 7 學生的姓名: zyh 學生的年齡: 16 學生的身份證號:1 8 學生的姓名: ouyangfeng 學生的年齡: 29 學生的身份證號:2 9 country china 10 address NewYork 11 student pupil 12 鍵是:France 值是:Romantic 13 鍵是:America 值是:Innovation 14 鍵是:China 值是:Reservation