JSP中c:foreach遍历和s:iterator遍历异同实例分析
本文实例分析了JSP中c:foreach遍历和s:iterator遍历的异同。分享给大家供大家参考。具体如下:
①jstlc:foreach
首先我们来看一个普通的servlet:
importcom.xy.entity.Board; importcom.xy.entity.Topic; importcom.xy.entity.User; publicclassToMainActionextendsHttpServlet { privateIBoarderDaoboardDao=newBoardDaoImpl(); privateITopicDaotopicDao=newTopicDaoImpl(); privateIUserDaouserDao=newUserDaoImpl(); publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException { //板块列表 List<Board>boards=boardDao.getAllBoard(); List<Integer>count=newArrayList<Integer>(); List<User>users=newArrayList<User>(); List<Topic>lastTopic=newArrayList<Topic>(); if(null!=boards) { for(Boardb:boards) { //回帖数 List<Topic>topic=topicDao.getTopicByBoardId(b.getborderId()); if(null!=topic) { intnum=topic.size(); count.add(num); } else { count.add(0); } //最近更新 Topict=topicDao.getLastTopic(b.getborderId()); lastTopic.add(t); //最近更新的作者 Useru=userDao.getUserByuId(t.getUid()); users.add(u); } request.setAttribute("boards",boards); request.setAttribute("count",count); request.setAttribute("users",users); request.setAttribute("lastTopic",lastTopic); RequestDispatcherdis=request.getRequestDispatcher("main.jsp"); dis.forward(request,response); } } publicvoiddoPost (HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException { this.doGet(request,response); } }
main.jsp:
<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%> <c:iftest="${requestScope.boards!=null}"> <c:forEachvar="b"items="${requestScope.boards}"varStatus="status"> <tr> <tdwidth="6%"height="68"> </td> <tdwidth="67%"> <divalign="left"class="blueSpan"> <imgsrc="images/topic.gif"width="18"height="21"/> <ahref="logined/ToListAction?boardId=${b.borderId}"> ${b.borderName}</a> </div> </td> <td> ${requestScope.count[status.index]} </td> <td> <palign="left"> ${requestScope.lastTopic[status.index].title} </p> <br/> <palign="left"> ${requestScope.users[status.index].userName} </p> <br/> <palign="left"> 修改时间: <br> ${requestScope.lastTopic[status.index].modifyTime} </p> <br/> </td> </tr> </c:forEach> </c:if>
②s:iterator
packagecom.xy.action; action publicclassToMainActionextendsActionSupportimplementsRequestAware { privateIBoarderDaoboardDao=newBoardDaoImpl(); privateITopicDaotopicDao=newTopicDaoImpl(); privateIUserDaouserDao=newUserDaoImpl(); privateMap<String,Object>request; publicvoidsetBoardDao(IBoarderDaoboardDao) { this.boardDao=boardDao; } publicvoidsetTopicDao(ITopicDaotopicDao) { this.topicDao=topicDao; } publicvoidsetUserDao(IUserDaouserDao) { this.userDao=userDao; } publicStringexecute() { //板块列表 List<Board>boards=boardDao.getAllBoard(); List<Integer>count=newArrayList<Integer>(); List<User>users=newArrayList<User>(); List<Topic>lastTopic=newArrayList<Topic>(); if(null!=boards) { for(Boardb:boards) { //回帖数 List<Topic>topic=topicDao.getTopicByBoardId(b.getBorderId()); if(null!=topic) { intnum=topic.size(); count.add(num); }else { count.add(0); } //最近更新 Topict=topicDao.getLastTopic(b.getBorderId()); lastTopic.add(t); //最近更新的作者 Useru=userDao.getUserByuId(t.getUid()); users.add(u); } request.put("boards",boards); request.put("count",count); request.put("users",users); request.put("lastTopic",lastTopic); } returnSUCCESS; } publicvoidsetRequest(Map<String,Object>request) { this.request=request; } }
main.jsp:
<%@tagliburi="/struts-tags"prefix="s"%> <s:iftest="#request.boards!=null"> <s:iteratorvalue="#request.boards"id="b"status="st"> <tr> <tdwidth="6%"height="68"> </td> <tdwidth="67%"> <divalign="left"class="blueSpan"> <imgsrc="images/topic.gif"width="18"height="21"/> <ahref="logined/ToListAction?boardId="+<s:propertyvalue="#b.borderId"/>+"> <s:propertyvalue="#b.borderName"/> </a> </div> </td> <td> <s:propertyvalue="#request.count[#st.index]"/> </td> <td> <br/> <palign="left"> <s:propertyvalue="#request.lastTopic[#st.index].title"/> </p> <br/> <palign="left"> <s:propertyvalue="#request.lastTopic[#st.index].userName"/> </p> <br/> <palign="left"> 修改时间: <br/> <s:propertyvalue="#request.lastTopic[#st.index].modifyTime"/> </p> <br/> </td> </tr> </s:iterator> </s:if>
希望本文所述对大家的JSP程序设计有所帮助。