JSP中c:foreach遍曆和s:iterator遍曆異同執行個體分析_JSP編程

來源:互聯網
上載者:User

本文執行個體分析了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.