欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-01-29 10:32:13
本文实例分析了jsp中c:foreach遍历和s:iterator遍历的异同。分享给大家供大家参考。具体如下: ①jstl c:foreach 首先我们来看一个普通的s...

本文实例分析了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;
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>

希望本文所述对大家的jsp程序设计有所帮助。