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;

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)
   {
    // 回帖数
    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>

目录
相关文章
|
1月前
|
开发者
迭代器(Iterator)遍历的两种方法(for和while)
迭代器(Iterator)遍历的两种方法(for和while)
|
11月前
遍历 ArrayList和遍历 Map的几种方式
遍历 ArrayList和遍历 Map的几种方式
48 0
ArrayList 三种遍历方法(for循环+下标、foreach、使用迭代器)
ArrayList 三种遍历方法(for循环+下标、foreach、使用迭代器)
|
Java 容器
使用Iterator遍历map以及list用法
使用Iterator遍历map以及list用法
148 0
使用Iterator遍历map以及list用法
|
JavaScript 索引
简单理解遍历器Iterator
简单理解遍历器Iterator
95 0
|
Scala 开发者
Map 的遍历 | 学习笔记
快速学习 Map 的遍历
85 0
|
缓存 Go 开发者
map 的遍历 | 学习笔记
快速学习 map 的遍历
86 0
Map的遍历方式
Map的遍历方式
123 0
遍历List集合的三种方法
遍历List集合的三种方法
156 0