This paper analyzes the similarities and differences of c:foreach traversal and s:iterator traversal in JSP. Share to everyone for your reference. Specifically as follows:
①jstl C:foreach
First, let's look at an ordinary 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 {//
Plate list 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) {//number of replies list<topic> Topic = Topicdao.gettopicbyboardid (b.getb
OrderId ());
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, Ioexcepti
On {this.doget (request, response);
}
}
Main.jsp:
<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%> <c:if "test=}" > <c:foreach var= "B" items= "${requestscope.boards}" varstatus= "status" > <tr> <td width= "6%" height= " > </td> <td width= "67%" > <div align= "left" class= "Bluespan" > <a href= "Logined/tolistaction?boardid=${b.borderid}" > ${b.bordername}</a> & Lt;/div> </td> <td> ${requestscope.count[status.index]} </td> <td> <p align= "left" &G
T ${requestscope.lasttopic[status.index].title} </p> <br/> <p align= "left" > ${requestscope.user S[status.index].username} </p> <br/> <p align= "left" > Modified: <br> ${requestscope.lastt
Opic[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 Board
Daoimpl ();
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 () {//Plate list 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) {//number of replies list<topic> Topic = Topicdao.gettopicbyboardid (b.getb
OrderId ());
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=", "> </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> <td> <s:property value= "#request. count[#st. index]"/> < /td> <td> <br/> <p align= "left" > <s:property value= "#request. lasttopic[#st. Index].title"/&
Gt
</p> <br/> <p align= "left" > <s:property value= "#request. lasttopic[#st. Index].username"/> </p> <br/> <p align= "Left" > Modify time: <br/> <s:property value= "#request. lasttopic[#st. Dex].modifytime "/> </p> <br/> </td> </tr> </s:iterator> </s:if>
I hope this article will help you with your JSP programming.