JSTL标签库遍历

遍历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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值