JSTL标签库遍历
2021/8/5 23:37:05
本文主要是介绍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>
这篇关于JSTL标签库遍历的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16Vue3资料:新手入门必读教程
- 2024-11-16Vue3资料:新手入门全面指南
- 2024-11-16Vue资料:新手入门完全指南
- 2024-11-16Vue项目实战:新手入门指南
- 2024-11-16React Hooks之useEffect案例详解
- 2024-11-16useRef案例详解:React中的useRef使用教程
- 2024-11-16React Hooks之useState案例详解
- 2024-11-16Vue入门指南:从零开始搭建第一个Vue项目
- 2024-11-16Vue3学习:新手入门教程与实践指南
- 2024-11-16Vue3学习:从入门到初级实战教程