[jsp]代码库
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="am_cn.itcast.domain.Student"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 1. 操作 javabean的 属性 , list 集合, map 集合, 数组 , 进行常规的 for循环操作
主要 用于 做 循环的 ,遍历 ,取出 目标 中 的 每个 元素 .
-->
<%
List list = new ArrayList();
list.add(new Student("李寒",18,"上海"));
list.add(new Student("孙在驰",19,"香港"));
list.add(new Student("李羿成",17,"东京"));
list.add(new Student("高杰", 19,"湖北"));
list.add(new Student("赵世龙",19,"巴黎"));
list.add(new Student("阿毛",19,"上海"));
list.add(new Student("宋勇",16,"深圳"));
request.setAttribute("list", list);
%>
<c:forEach items="${list }" var="student" >
${student.name } ${student.age } ${student.city } <br/>
</c:forEach>
<hr/>
<c:forEach items="${list }" var="student" >
${student.name } ${student.age } ${student.city } <br/>
</c:forEach>
<table border="1" align="center" width="70%">
<tr>
<td>索引</td>
<td>是否是第一个</td>
<td>是否是最后一个 </td>
<td>姓名</td>
<td>年龄</td>
<td>城市</td>
</tr>
<c:forEach items="${list }" var="student" varStatus="vs">
<tr bgcolor="${vs.index%2==0?"#EBEBEB":"#F8D890" }">
<td>${vs.index }</td>
<td>${vs. first}</td>
<td>${vs.last }</td>
<td>${student.name }</td>
<td>${student.age } </td>
<td>${student.city }</td>
</tr>
</c:forEach>
</table>
<hr/>
<!-- 遍历 map 集合,取出 map 集合中的 数据 -->
<%
Map map = new LinkedHashMap();
map.put("aaa", "111");
map.put("bbb", "222");
map.put("ccc", "333");
request.setAttribute("map", map);
%>
<c:forEach items="${map }" var="entry">
${entry.key } ${entry.value } <br/>
</c:forEach>
<!-- 遍历 数组 -->
<%
String[] names ={"张柏芝","谢霆锋","王菲","文章"};
request.setAttribute("names", names);
%>
<c:forEach items="${names }" var="name">
${name }
</c:forEach>
<!-- 进行常规的 for循环操作 -->
<%
// 求 1+2+3... + 9 的 和
int sum = 0;
for(int i=1;i<=9;i++){
sum+=i;
}
out.print(sum);
%>
<!-- 用 foreach 循环 怎么 做 ? -->
<c:forEach begin="1" end="9" step="1" var="i">
<c:set var="result" value="${result+i }"></c:set>
</c:forEach>
${result }
</body>
</html>