最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2015年09月14日 10:08:16   作者:異次元藍(lán)客  
這篇文章主要介紹了JSP中c:foreach遍歷和s:iterator遍歷異同,以兩個(gè)完整實(shí)例形式對比分析了c:foreach遍歷和s:iterator遍歷的具體用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例分析了JSP中c:foreach遍歷和s:iterator遍歷的異同。分享給大家供大家參考。具體如下:

①jstl c:foreach

首先我們來看一個(gè)普通的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)
  {
  // 回帖數(shù)
  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">
  修改時(shí)間:
  <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 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)
  {
  // 回帖數(shù)
  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">
  修改時(shí)間:
 <br/>
  <s:property value=" #request.lastTopic[#st.index].modifyTime" />
  </p>
  <br />
 </td>
 </tr>
    </s:iterator>
</s:if>

希望本文所述對大家的JSP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • jsp超鏈接中文亂碼的解決方法

    jsp超鏈接中文亂碼的解決方法

    jsp超鏈接中文亂碼的解決方法,需要的朋友可以參考一下
    2013-04-04
  • JSP頁面間的傳值方法總結(jié)

    JSP頁面間的傳值方法總結(jié)

    JSP頁面間傳遞參數(shù)是經(jīng)常需要使用到的功能,有時(shí)還需要多個(gè)JSP頁面間傳遞參數(shù),下面這篇文章主要給大家介紹了關(guān)于JSP頁面間傳值方法的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看吧。
    2017-07-07
  • 用JSP編寫文件上傳

    用JSP編寫文件上傳

    用JSP編寫文件上傳...
    2006-10-10
  • struts2 session 解讀

    struts2 session 解讀

    session 在struts2中的用法
    2009-06-06
  • jsp給后臺(tái)帶多個(gè)參數(shù)的方法

    jsp給后臺(tái)帶多個(gè)參數(shù)的方法

    下面小編就為大家?guī)硪黄猨sp給后臺(tái)帶多個(gè)參數(shù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • JSP 中spring事務(wù)配置詳解

    JSP 中spring事務(wù)配置詳解

    這篇文章主要介紹了JSP 中spring事務(wù)配置詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • JSP 中Session的詳解及原理分析

    JSP 中Session的詳解及原理分析

    這篇文章主要介紹了JSP 中Session的詳細(xì)介紹的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握Session的用法,需要的朋友可以參考下
    2017-10-10
  • 簡單的手工hibernate程序示例

    簡單的手工hibernate程序示例

    這篇文章主要介紹了簡單的手工hibernate程序示例,以實(shí)例形式分析了簡單hibernate程序的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • js實(shí)現(xiàn)隨機(jī)的四則運(yùn)算題目效果

    js實(shí)現(xiàn)隨機(jī)的四則運(yùn)算題目效果

    本文主要介紹了js實(shí)現(xiàn)隨機(jī)的四則運(yùn)算題目效果,可以隨機(jī)的進(jìn)行加減乘除的運(yùn)算,有興趣的同學(xué)可以了解一下。
    2016-10-10
  • SpringMVC上傳文件的簡單實(shí)例

    SpringMVC上傳文件的簡單實(shí)例

    這篇文章主要介紹了SpringMVC上傳文件的簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06

最新評(píng)論

来宾市| 阿拉善盟| 泰和县| 吉首市| 陕西省| 瑞安市| 五家渠市| 宜川县| 万源市| 新源县| 兰坪| 施秉县| 乐山市| 梁平县| 依安县| 喜德县| 克东县| 仪陇县| 太谷县| 定襄县| 色达县| 拜泉县| 广州市| 巴林左旗| 大名县| 区。| 烟台市| 庆城县| 眉山市| 宁远县| 阿克| 定襄县| 新巴尔虎左旗| 高州市| 皋兰县| 丰宁| 玉田县| 龙陵县| 绥芬河市| 安顺市| 德江县|