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

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.