时间:2021-05-26
本文实例分析了JSP中c:foreach遍历和s:iterator遍历的异同。分享给大家供大家参考。具体如下:
①jstl c:foreach
首先我们来看一个普通的servlet:
import com.xy.entity.Board;import com.xy.entity.Topic;import com.xy.entity.User;public class ToMainAction extends HttpServlet{ private IBoarderDao boardDao = new BoardDaoImpl(); private ITopicDao topicDao = new TopicDaoImpl(); private IUserDao userDao = new UserDaoImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { // 板块列表 List<Board> boards = boardDao.getAllBoard(); List<Integer> count = new ArrayList<Integer>(); List<User> users = new ArrayList<User>(); List<Topic> lastTopic = new ArrayList<Topic>(); if (null != boards) { for (Board b : boards) { // 回帖数 List<Topic> topic = topicDao.getTopicByBoardId(b.getborderId()); if(null!=topic) { int num = topic.size(); count.add(num); } else { count.add(0); } // 最近更新 Topic t = topicDao.getLastTopic(b.getborderId()); lastTopic.add(t); // 最近更新的作者 User u = userDao.getUserByuId(t.getUid()); users.add(u); } request.setAttribute("boards", boards); request.setAttribute("count", count); request.setAttribute("users", users); request.setAttribute("lastTopic", lastTopic); RequestDispatcher dis = request.getRequestDispatcher("main.jsp"); dis.forward(request, response); } } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}main.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><c:if test="${requestScope.boards!=null}"> <c:forEach var="b" items="${requestScope.boards}" varStatus="status"> <tr> <td width="6%" height="68"> </td> <td width="67%"> <div align="left" class="blueSpan"> <img src="images/topic.gif" width="18" height="21" /> <a href="logined/ToListAction?boardId=${b.borderId}"> ${b.borderName}</a> </div> </td> <td> ${requestScope.count[status.index]} </td> <td> <p align="left"> ${requestScope.lastTopic[status.index].title} </p> <br /> <p align="left"> ${requestScope.users[status.index].userName} </p> <br /> <p align="left"> 修改时间: <br> ${requestScope.lastTopic[status.index].modifyTime} </p> <br /> </td> </tr> </c:forEach></c:if>②s:iterator
package com.xy.action;actionpublic class ToMainAction extends ActionSupport implements RequestAware{ private IBoarderDao boardDao = new BoardDaoImpl(); private ITopicDao topicDao = new TopicDaoImpl(); private IUserDao userDao = new UserDaoImpl(); private Map<String, Object> request; public void setBoardDao(IBoarderDao boardDao) { this.boardDao = boardDao; } public void setTopicDao(ITopicDao topicDao) { this.topicDao = topicDao; } public void setUserDao(IUserDao userDao) { this.userDao = userDao; } public String execute() { // 板块列表 List<Board> boards = boardDao.getAllBoard(); List<Integer> count = new ArrayList<Integer>(); List<User> users = new ArrayList<User>(); List<Topic> lastTopic = new ArrayList<Topic>(); if (null != boards) { for (Board b : boards) { // 回帖数 List<Topic> topic = topicDao.getTopicByBoardId(b.getBorderId()); if (null != topic) { int num = topic.size(); count.add(num); } else { count.add(0); } // 最近更新 Topic t = topicDao.getLastTopic(b.getBorderId()); lastTopic.add(t); // 最近更新的作者 User u = userDao.getUserByuId(t.getUid()); users.add(u); } request.put("boards", boards); request.put("count", count); request.put("users", users); request.put("lastTopic", lastTopic); } return SUCCESS; } public void setRequest(Map<String, Object> request) { this.request = request; }}main.jsp:
<%@ taglib uri="/struts-tags" prefix="s"%><s:if test="#request.boards!=null"> <s:iterator value="#request.boards" id="b" status="st"> <tr> <td width="6%" height="68"> </td> <td width="67%"> <div align="left" class="blueSpan"> <img src="images/topic.gif" width="18" height="21" /> <a href="logined/ToListAction?boardId="+<s:property value="#b.borderId"/>+"> <s:property value="#b.borderName" /> </a> </div> </td> <td> <s:property value=" #request.count[#st.index]" /> </td> <td> <br /> <p align="left"> <s:property value="#request.lastTopic[#st.index].title" /> </p> <br /> <p align="left"> <s:property value=" #request.lastTopic[#st.index].userName" /> </p> <br /> <p align="left"> 修改时间: <br/> <s:property value=" #request.lastTopic[#st.index].modifyTime" /> </p> <br /> </td> </tr> </s:iterator></s:if>希望本文所述对大家的JSP程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了ES6中Iterator与for..of..遍历用法。分享给大家供大家参考,具体如下:Iterator与for..of..遍历1.Iterator
本文实例讲述了C#中foreach语句使用break暂停遍历的方法。分享给大家供大家参考。具体分析如下:下面的代码演示了在C#中使用foreach时如何通过br
foreach遍历是C#常见的功能,而本文通过实例形式展现了C#使用yield关键字让自定义集合实现foreach遍历的方法。具体步骤如下:一般来说当我们创建自
本文实例讲述了C#使用foreach语句遍历二维数组的方法。分享给大家供大家参考。具体分析如下:如果通过for语句循环遍历二维数组需要两重循环才可以,二fore
本文实例讲述了C#使用foreach语句搜索数组元素的方法。分享给大家供大家参考。具体分析如下:下面的代码通过foreach语句对数组遍历,然后对元素进行逐个比