遍历1
<%--
1.遍历1到10,输出
begin属性设置开始的索引
end属性设置结束的索引
var属性表示循环的变量
for(int i=1;i<10;i++)
--%>
<table border="1">
<c:forEach begin="1" end="10" var="i">
<tr>
<td>第${i}行</td>
</tr>
</c:forEach>
</table>
<hr>
2.遍历object数组
<%--
遍历object数组
for(object item:arr)
items表示遍历的数据源(遍历的集合)
var 表示当前遍历到的数据
--%>
<%
request.setAttribute("arr",new String[]{"156654","481684","5746846"});
%>
<c:forEach items="${requestScope.arr}" var="item">
${item} <br>
</c:forEach>
<hr>
3.遍历map集合
<%
Map<String,Object> map=new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
//for(Map.Entry<String,Object> entry:map.entrySet()){}
request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var="entry">
<h1>${entry.key} = ${entry.value}</h1>
</c:forEach>
4.遍历List集合
list中存放Student类,有属性:编号,用户名,密码,年龄,电话信息–%>
public class Student {
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
public Student() {
}
public Student(Integer id, String username, String password, Integer age, String phone) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}';
}
}
<%
List<Student> studentList=new ArrayList<>();
for (int i=1;i<=10;i++){
studentList.add(new Student(i,"username"+i,"password"+i,18+i,"phone"+i));
}
request.setAttribute("stus",studentList);
%>
<table>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
<th>年龄</th>
<th>电话</th>
<th>操作</th>
</tr>
<c:forEach items="${requestScope.stus}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.username}</td>
<td>${stu.password}</td>
<td>${stu.age}</td>
<td>${stu.phone}</td>
<td>删除,修改</td>
</tr>
</c:forEach>
</table>