litems属性:指定要遍历的集合,—般使用EL表达式来赋值lvar属性:指定一个绑定名称,容器每次从集合中取一个对象,然后绑定到pageContext对象上lvarStatus属性:指定一个绑定名称,绑定值是一个由容器创建的对象,该对象封装了当前迭代的状态
工具/原料
eclipse
方法/步骤
1、<%@pagelanguage="java"contentType="text/html;charset=utf-8"pageEncoding="utf-8"import="java.util.*"%><%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%><html><head><styletype="text/css">.row1{background-color:red}.row2{background-color:green}</style></head>
2、<body><tableborder="1px衡痕贤伎soliderred"style="稆糨孝汶;text-align:center;"cellspacing="0"cellpadding="10"width="50%"><thead><tr><td>count</td><td>index</td><td>姓名</td><td>年龄</td><td>分数</td></tr></thead><tbody><c:forEachitems="${users}"var="u"varStatus="s"><trclass="row${s.count%2+1}"><td>${s.count}</td><td>${s.index}</td><td>${u.name}</td><td>${u.age}</td><td>${u.score}</td>
3、</tr></c:forEach><c:forEachitems="${users}"var='u'varStatus="s"><tr><td>${s.count}</td><td>${s.index}</td><td>${u.name}</td><td>${u.age}</td><td>${u.score}</td></tr></c:forEach></tbody></table></body></html>
4、packagebean;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;
5、/***ServletimplementationclassActionServlet*/publicclassActionServletextendsHttpServlet{//bootstrap3.Xprotectedvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringpath=request.getRequestURI();Stringaction=path.substring(path.lastIndexOf("/"),path.lastIndexOf("."));
6、//System.out.println(action);if(action.equalsIgn艺皱麾酪oreCase("/f足毂忍珩indAll")){List<User>users=newArrayList<User>();for(inti=0;i<15;i++){Useruser=newUser();user.setAge(23+i);user.setName("zs"+i);user.setScore(67+i);users.add(user);}request.setAttribute("users",users);request.getRequestDispatcher("jstlDemo3.jsp").forward(request,response);}}}