In JSP, c: foreach traversal and s: iterator traverse Similarities and Differences instance analysis, foreachiterator
This article analyzes the similarities and differences between c: foreach traversal and s: iterator traversal in JSP. Share it with you for your reference. The details are as follows:
① Jstl c: foreach
First, let's look at a common 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 of sections <B Oard> 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) {// number of replies List <Topic> topic = topicDao. getTopicByBoardId (B. getborderId (); if (null! = Topic) {int num = topic. size (); count. add (num);} else {count. add (0);} // recently updated Topic t = topicDao. getLastTopic (B. getborderId (); lastTopic. add (t); // recently updated author 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"> <a href = "logined/ToListAction? BoardId =$ {B. borderId} ">$ {B. borderName} </a> </div> </td> <td >$ {requestScope. count [status. index]} </td> <p align = "left" >$ {requestScope. lastTopic [status. index]. title} </p> <br/> <p align = "left" >$ {requestScope. users [status. index]. userName} </p> <br/> <p align = "left"> modification time: <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 inline (); private IUserDao userDao = new UserDaoImpl (); private Map <String, Object> request; public void setBoardDao (IBoarderDao boardDao) {this. boardDao = boardDao;} public void setTopicDao (ITopicDao topicDa O) {this. topicDao = topicDao;} public void setUserDao (IUserDao userDao) {this. userDao = userDao;} public String execute () {// List of sections <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) {// number of replies List <Topic> topic = topicDao. getTopicByBoardId (B. getBorderId (); if (null! = Topic) {int num = topic. size (); count. add (num);} else {count. add (0);} // recently updated Topic t = topicDao. getLastTopic (B. getBorderId (); lastTopic. add (t); // recently updated author 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"> <a href =" logined/ToListAction? BoardId = "+ <s: property value =" # B. borderId "/> +"> <s: property value = "# B. borderName "/> </a> </div> </td> <s: property value =" # request. count [# st. index] "/> </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 "> modification time: <br/> <s: property value =" # request. lastTopic [# st. index]. modifyTime "/> </p> <br/> </td> </tr> </s: iterator> </s: if>
I hope this article will help you with JSP program design.