JSTL_常用标签_foreach
foreach:相当于Java代码的for
1.完成重复的操作
for(int i = 0; i<10; i++){
}
属性:
begin:开始值
end:结束值
var:临时变量
step:步长
varStatus:循环状态对象
index:容器中元素的索引,从0开始
count:循环次数,从1开始
2.遍历容器
List<User> list;
for(User user : list){
}
属性:
items:容器对象
var:容器中元素的临时变量
varStatus:循环状态对象
index:容器中元素的索引,从0开始
count:循环次数,从1开始
<%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>foreach标签</title> </head> <body> <%-- foreach:相当于Java代码的for 1.完成重复的操作 for(int i = 0; i<10; i++){ } 属性: begin:开始值 end:结束值 var:临时变量 step:步长 varStatus:循环状态对象 index:容器中元素的索引,从0开始 count:循环次数,从1开始 2.遍历容器 List<User> list; for(User user : list){ } 属性: items:容器对象 var:容器中元素的临时变量 varStatus:循环状态对象 index:容器中元素的索引,从0开始 count:循环次数,从1开始 --%> <c:forEach begin="1" end="10" var="i" step="2" varStatus="s"> ${i} <h3>${s.index} </h3> <h4> ${s.count}</h4> <br> </c:forEach> <hr> <% List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); request.setAttribute("list",list); %> <c:forEach items="${list}" var="str" varStatus="s"> ${s.index} ${s.count} ${str}<br> </c:forEach> </body> </html>
JSTL_练习
在request域中有一个存在User对象的集合需要使用jstl将list集合展示数据展示到jsp页面的表格table中
<%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Date" %> <%@ page import="fu.xueqiang.domain.User" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>test</title> </head> <body> <% List list = new ArrayList(); list.add(new User("张三",23,new Date())); list.add(new User("李四",24,new Date())); list.add(new User("王五",25,new Date())); request.setAttribute("list",list); %> <table border="1" width="500" align="center"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>生日</th> </tr> <%--数据行--%> <c:forEach items="${list}" var="user" varStatus="s"> <c:if test="${s.count % 2 != 0}"> <tr bgcolor="red"> <td>${s.count}</td> <td>${user.name}</td> <td>${user.age}</td> <td>${user.birthday}</td> </tr> </c:if> <c:if test="${s.count % 2 == 0}"> <tr bgcolor="green"> <td>${s.count}</td> <td>${user.name}</td> <td>${user.age}</td> <td>${user.birthday}</td> </tr> </c:if> </c:forEach> </table> </body> </html>
package fu.xueqiang.domain; import java.util.Date; public class User { private String name; private int age; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public User(String name, int age, Date birthday) { this.name = name; this.age = age; this.birthday = birthday; } public User() { } }
标签:count,name,标签,age,JSTL,birthday,foreach,public,user From: https://www.cnblogs.com/x3449/p/17113908.html