主页 > 知识库 > JSP中c:foreach遍历和s:iterator遍历异同实例分析

JSP中c:foreach遍历和s:iterator遍历异同实例分析

热门标签:杭州ai语音电销机器人功能 电销机器人好卖么 电销机器人是有一些什么技术 四川保险智能外呼系统商家 高德地图标注样式 杭州语音电销机器人软件 地图标注线上教程 北票市地图标注 商洛电销

本文实例分析了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
 {
 // 板块列表
 ListBoard> boards = boardDao.getAllBoard();
 ListInteger> count = new ArrayListInteger>();
 ListUser> users = new ArrayListUser>();
 ListTopic> lastTopic = new ArrayListTopic>();
 if (null != boards)
 {
  for (Board b : boards)
  {
  // 回帖数
  ListTopic> 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;
action
public class ToMainAction extends ActionSupport implements RequestAware
{
 private IBoarderDao boardDao = new BoardDaoImpl();
 private ITopicDao topicDao = new TopicDaoImpl();
 private IUserDao userDao = new UserDaoImpl();
 private MapString, 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()
 {
 // 板块列表
 ListBoard> boards = boardDao.getAllBoard();
 ListInteger> count = new ArrayListInteger>();
 ListUser> users = new ArrayListUser>();
 ListTopic> lastTopic = new ArrayListTopic>();
 if (null != boards)
 {
  for (Board b : boards)
  {
  // 回帖数
  ListTopic> 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(MapString, 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程序设计有所帮助。

您可能感兴趣的文章:
  • 小议JavaScript中Generator和Iterator的使用
  • 深入解读JavaScript中的Iterator和for-of循环
  • JavaScript实现Iterator模式实例分析
  • 如何实现java Iterator迭代器功能
  • Java Iterator接口遍历单列集合迭代器原理详解
  • PHP设计模式之迭代器(Iterator)模式入门与应用详解
  • Java集合使用 Iterator 删除元素
  • Python迭代器iterator生成器generator使用解析
  • JavaScript设计模型Iterator实例解析

标签:红河 丹东 贵州 宿州 云浮 江西 西藏 青岛

巨人网络通讯声明:本文标题《JSP中c:foreach遍历和s:iterator遍历异同实例分析》,本文关键词  JSP,中,foreach,遍历,和,iterator,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《JSP中c:foreach遍历和s:iterator遍历异同实例分析》相关的同类信息!
  • 本页收集关于JSP中c:foreach遍历和s:iterator遍历异同实例分析的相关信息资讯供网民参考!
  • 推荐文章