SoFunction
Updated on 2025-04-06

Analysis of similarities and differences in c:foreach traversal and s:iterator traversal in JSP

This article analyzes the similarities and differences between c:foreach traversal and s:iterator traversal in JSP. Share it for your reference. The details are as follows:

①jstl c:foreach

First, let's look at an ordinary servlet:

import ;
import ;
import ;
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
 {
 // Section list List<Board> boards = ();
 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)
  {
  // Number of replies  List<Topic> topic = (());
  if(null!=topic)
  {
   int num = ();
   (num);
  }
  else
  {
   (0);
  }
  // Recently updated  Topic t = (());
  (t);
  // Recently updated author  User u = (());
  (u);
  }
  ("boards", boards);
  ("count", count);
  ("users", users);
  ("lastTopic", lastTopic);
  RequestDispatcher dis = ("");
  (request, response);
 }
 }
 public void doPost
    (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
 (request, response);
 }
}

:

<%@ taglib uri="/jsp/jstl/core" prefix="c"%>
<c:if test="${!=null}">
 <c:forEach var="b" items="${}" varStatus="status">
 <tr>
 <td width="6%" height="68">
 </td>
 <td width="67%">
  <div align="left" class="blueSpan">
  <img src="images/" width="18" height="21" />
  <a href="logined/ToListAction?boardId=${}">
  ${}</a>
  </div>
 </td>
 <td>
  ${[]}
 </td>
 <td>
  <p align="left">
  ${[].title}
  </p>
  <br />
  <p align="left">
    ${[].userName}
  </p>
  <br />
  <p align="left">
  Modification time:
  <br>
  ${[].modifyTime}
  </p>
  <br />
 </td>
 </tr>
 </c:forEach>
</c:if>

②s:iterator

package ;
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)
 {
  = boardDao;
 }
 public void setTopicDao(ITopicDao topicDao)
 {
  = topicDao;
 }
 public void setUserDao(IUserDao userDao)
 {
  = userDao;
 }
 public String execute()
 {
 // Section list List<Board> boards = ();
 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)
  {
  // Number of replies  List<Topic> topic = (());
  if (null != topic)
  {
   int num = ();
   (num);
  } else
  {
   (0);
  }
  // Recently updated  Topic t = (());
  (t);
  // Recently updated author  User u = (());
  (u);
  }
  ("boards", boards);
  ("count", count);
  ("users", users);
  ("lastTopic", lastTopic);
 }
 return SUCCESS;
 }
 public void setRequest(Map<String, Object> request)
 {
  = request;
 }
}

<%@ taglib uri="/struts-tags" prefix="s"%>
<s:if test="#!=null">
 <s:iterator value="#"  status="st">
 <tr>
 <td width="6%" height="68">
 </td>
 <td width="67%">
   <div align="left" class="blueSpan">
  <img src="images/" width="18" height="21" />
  <a href="logined/ToListAction?boardId="+<s:property value="#"/>+">
   <s:property value="#" />
  </a>
  </div>
 </td>
 <td>
  <s:property value=" #[#]" />
 </td>
 <td>
 <br />
  <p align="left">
  <s:property value="#[#].title" />
  </p>
 <br />
  <p align="left">
  <s:property value=" #[#].userName" />
  </p>
 <br />
  <p align="left">
  Modification time:
 <br/>
  <s:property value=" #[#].modifyTime" />
  </p>
  <br />
 </td>
 </tr>
    </s:iterator>
</s:if>

I hope this article will be helpful to everyone's JSP programming.