濮阳杆衣贸易有限公司

主頁 > 知識庫 > JSP中c:foreach遍歷和s:iterator遍歷異同實例分析

JSP中c:foreach遍歷和s:iterator遍歷異同實例分析

熱門標簽:杭州ai語音電銷機器人功能 電銷機器人好賣么 電銷機器人是有一些什么技術 四川保險智能外呼系統(tǒng)商家 高德地圖標注樣式 杭州語音電銷機器人軟件 地圖標注線上教程 北票市地圖標注 商洛電銷

本文實例分析了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)
  {
  // 回帖數(shù)
  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)
  {
  // 回帖數(shù)
  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循環(huán)
  • JavaScript實現(xiàn)Iterator模式實例分析
  • 如何實現(xiàn)java Iterator迭代器功能
  • Java Iterator接口遍歷單列集合迭代器原理詳解
  • PHP設計模式之迭代器(Iterator)模式入門與應用詳解
  • Java集合使用 Iterator 刪除元素
  • Python迭代器iterator生成器generator使用解析
  • JavaScript設計模型Iterator實例解析

標簽:紅河 丹東 貴州 宿州 云浮 江西 西藏 青島

巨人網(wǎng)絡通訊聲明:本文標題《JSP中c:foreach遍歷和s:iterator遍歷異同實例分析》,本文關鍵詞  JSP,中,foreach,遍歷,和,iterator,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《JSP中c:foreach遍歷和s:iterator遍歷異同實例分析》相關的同類信息!
  • 本頁收集關于JSP中c:foreach遍歷和s:iterator遍歷異同實例分析的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    安国市| 台湾省| 孙吴县| 萨迦县| 郓城县| 十堰市| 隆回县| 西平县| 陆川县| 郸城县| 渑池县| 谷城县| 富裕县| 麻江县| 观塘区| 武宁县| 新宾| 敦化市| 衡东县| 柯坪县| 新巴尔虎右旗| 舟山市| 万宁市| 时尚| 磐安县| 咸丰县| 永顺县| 苍梧县| 梁山县| 吴江市| 肇东市| 黄大仙区| 论坛| 班戈县| 乐陵市| 松原市| 弥勒县| 连云港市| 邛崃市| 靖州| 济阳县|