JSP中c:foreach遍历和s:iterator遍历异同实例分析
网络编程 2021-07-05 11:23www.168986.cn编程入门
这篇文章主要介绍了JSP中c:foreach遍历和s:iterator遍历异同,以两个完整实例形式对比分析了c:foreach遍历和s:iterator遍历的具体用法,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例分析了JSP中c:foreach遍历和s:iterator遍历的异同。分享给大家供大家参考。具体如下
①jstl c:foreach
我们来看一个普通的servlet:
import .xy.entity.Board; import .xy.entity.Topic; import .xy.entity.User; public class ToMainAction extends HttpServlet { private IBoarderDao boardDao = new BoardDaoImpl(); private ITopicDao icDao = 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> ic = icDao.getTopicByBoardId(b.getborderId()); if(null!=ic) { int num = ic.size(); count.add(num); } else { count.add(0); } // 最近更新 Topic t = icDao.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./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/ic.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 .xy.action; action public class ToMainAction extends ActionSupport implements RequestAware { private IBoarderDao boardDao = new BoardDaoImpl(); private ITopicDao icDao = new TopicDaoImpl(); private IUserDao userDao = new UserDaoImpl(); private Map<String, Object> request; public void setBoardDao(IBoarderDao boardDao) { this.boardDao = boardDao; } public void setTopicDao(ITopicDao icDao) { this.icDao = icDao; } 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> ic = icDao.getTopicByBoardId(b.getBorderId()); if (null != ic) { int num = ic.size(); count.add(num); } else { count.add(0); } // 最近更新 Topic t = icDao.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/ic.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程序设计有所帮助。
上一篇:JSP学习之数据库开发小结
下一篇:struts2的select标签用法实例分析
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程