主页 > 编程资料 > JSP(JAVA) >
发布时间:2016-01-01 作者:网络 阅读:185次

本文实例分析了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 boards = boardDao.getAllBoard();
 List count = new ArrayList();
 List users = new ArrayList();
 List lastTopic = new ArrayList();
 if (null != boards)
 {
  for (Board b : boards)
  {
  // 回帖数
  List 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"%>

 
 
 
 
 
  
  
  
  ${b.borderName}
  <>
 
 
  ${requestScope.count[status.index]}
 
 
  

${requestScope.lastTopic[status.index].title}


${requestScope.users[status.index].userName}


修改时间:
${requestScope.lastTopic[status.index].modifyTime}


②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 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 boards = boardDao.getAllBoard();
 List count = new ArrayList();
 List users = new ArrayList();
 List lastTopic = new ArrayList();
 if (null != boards)
 {
  for (Board b : boards)
  {
  // 回帖数
  List 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 request)
 {
 this.request = request;
 }
}

main.jsp:

<%@ taglib uri="/struts-tags" prefix="s"%>

 
 
 
 
 
   
  
  +">
   
  
  <>
 
 
  
 
 
 



修改时间:


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

关键字词:JSP编程  

必填

◎已有0人评论

加载更多