logic:Iterator标签(转)

logic:Iterator标签(以下简称“该标签”)是Struts里非常常用的一个标签,其作用在于循环显示给定容器对象中的值。
  
  如此常用的标签,其源代码当然需要拿出来研究一下,以下列举几条研究成果:
  
  1、该标签内部使用Collection来表示给定的容器,所有的给定容器对象(如ArrayList,Map等)都会被其转化成为Collection,Collection实际就是Map和List的父类。
  
  2、该标签自己维护循环索引,不用程序员管理索引
  
  3、该标签常见的几个属性如下:name、property、scope、id
  
  对应Struts给出的Api说明如下:
  
  name:包括要遍历Collection的Jsp页面的bean的名字(如果property没有被定义),或者是那些通过getter方法获得属性的Jsp中的Bean的名字,这些getter方法返回的是Collection(如果property定义了)。
  
  property:在name命名的Jsp bean中定义的属性的名字,通过getter方法返回一个Collection
  
  scope:指示到哪里去寻找name为名字的bean,如果没有定义缺省为"any scope"
  
  id:如果Collection非空的话,在每次遍历时候Collection中每个元素的名字。
  
  其中除了id每个元素均为Rt expr,这儿的rt expr的意思就是Run Time Expression。明确的说就是,如果你对一个Attribute的<rtexprvalue>指定为true,你就可以在这样的属性中使用<%=%>之类的东东。这个配置文件在tld中。
  
  只有id是必须要说明的。
  
  关于Api说明的说明:
  
  id只是一个临时标识,在下面的<bean:write里面出现的name属性要和id一致才能打印出<bean:write的property,而此property就是在iterator中的属性。
  
  举例说明
  
  以下代码生成一个阶梯状表格
  
  系统 资源 操作
  
  soft3
  res3
  opt3
  soft12
  res12
  opt1211
  soft11
  res11
  opt1111
  
  在此之前传来一个request.getAttribute("userPurview"),所以有在第一个logic中的userPurview,就是在这个request里面寻找userPurview
  
  返回的是一个list
  
  <table width="300" border="0">
  <tr><td>系统</td>
  <td>资源</td>
  <td>操作</td>
  </tr>
  <logic:iterate id="targetSys" name="userPurview" scope="request"> //这个id可以随便起名,但是要注意下文使用的一致性
  <tr bgcolor="#cccccc"><td height="21" class="unnamed2">
  <bean:write name="targetSys" property="cn"/>    //此处name和上面id保持一致,property就是第一个list里面的元素
  </td>
  <td height="21" class="unnamed2"> </td>
  <td height="21" class="unnamed3"> </td>
  </tr>
  <logic:iterate id="targetRes" name="targetSys" property="purviewResList">
  <tr><td height="21" class="unnamed2"> </td><td height="21" class="unnamed5">
  <bean:write name="targetRes" property="cn"/>
  </td>
  <td height="21" class="unnamed6"> </td>
  </tr>
  
  <logic:iterate id="targetOpr" name="targetRes" property="purviewOprList">
  <tr><td height="21" class="unnamed4"> </td><td height="21" class="unnamed4"> </td>
  <td height="21" class="redzi">
  <bean:write property="cn" name="targetOpr"/></td>
  </tr>
  </logic:iterate>
  
  </logic:iterate>
  
  </logic:iterate>
  </table>
  
  结论:
  
  多级迭代和单层差不多,唯一注意的就是id和<bean:write中的name的对应,上级logic的id与下级logic的name对应,并且取出来的要是个Collection,name和id不一定实际需要这个bean,都是虚拟的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我就是靠这个文档实现logic:iterate的循环的<br>struts 标签 logic:iterate使用 logic:iterate<br><br>第一页 是struts官方的说明,<br>第二页 是个例子<br>第三页 是我实现的arrayList放入标签的方法。<br>这是页面文件<br><%@ page language="java"<br> import="java.util.*,cn.edu.bit.zgc2d.accountQuery.*" pageEncoding="GBK"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"<br> prefix="bean"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-html"<br> prefix="html"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"<br> prefix="logic"%><br><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br><html><br> <head><br> </head><br> <body><br> <table><br> <tr><br> <td><%@ include file="../menu.txt"%><br> </td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=100 width=30><br> <img src=../images/small_2.gif><br> </td><br> <td><br> 账务查询<br> <br><br> 基本信息查询。<br> </td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=1 width=700 bgcolor=gray colspan=2></td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=100 width=30><br> <img src=../images/small_2.gif><br> </td><br> <td><br> 基本信息查询。<br> </td><br> </tr><br> </table><br> <table><br> <tr><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 帐号<br> </td><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 帐户别名<br> </td><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 是否为主帐户<br> </td><br> </tr><br> <logic:iterate id="item" name="list" indexId="index"><br> <tr><br> <td WIDTH=100 bgcolor=F2F2F2><br> <a href="www.sina.com.cn?abc=<bean:write name="item" property="account" />"><bean:write name="item" property="account" /><br> </td><br> <td WIDTH=100 bgcolor=F2F2F2><br> <bean:write name="item" property="other_name" /><br> </td><br> <td WIDTH=100 bgcolor=F2F2F2><br> <bean:write name="item" property="is_main_account" /><br> </td><br> </tr><br> </logic:iterate><br> </table><br> </body><br></html><br>这是action<br>public class InformationAction extends Action {<br> public ActionForward execute(ActionMapping mapping, ActionForm form,<br> HttpServletRequest request, HttpServletResponse response) {<br> InformationForm informationForm = (InformationForm) form;// TODO Auto-generated method stub<br> //业务开始<br> Connection conn = null; Statement st = null; ResultSet rs = null;<br> try {<br> db db = new db();<br> // 打开数据库<br> conn = db.conn();<br> // 创建st<br> st = conn.createStatement();<br> // 组织sql并执行<br> HttpSession session_account = request.getSession();<br> String account = (String) session_account.getAttribute("account");<br> String sql = "select * from account where main_account=" + account<br> + "order by is_main_account desc";<br> rs = st.executeQuery(sql);<br> // 组建arrayList列表<br> ArrayList list = new ArrayList();<br> while (rs.next()) {<br> accountBean bean = new accountBean();<br> bean.setArea(rs.getString("area")); bean.setKind(rs.getInt("kind")); bean.setAccount(rs.getString("account")); bean.setMain_account(rs.getString("main_account")); bean.setType(rs.getInt("type")); bean.setOther_name(rs.getString("other_name")); bean.setPassword(rs.getString("password")); bean.setIs_main_account(rs.getInt("is_main_account")); bean.setMoney(rs.getString("money")); bean.setId(rs.getInt("id"));<br> list.add(bean);<br> }<br> HttpSession session = request.getSession();<br> session.setAttribute("list", list);<br> return mapping.findForward("success");<br> } catch (Exception e) { e.printStackTrace(); } <br> //业务结束<br> return null;<br> }<br>}//自己将需要的包导入<br>bean文件<br>package cn.edu.bit.zgc2d.accountQuery;<br><br>public class accountBean {<br> private String area;<br> private int kind;<br> private String account;<br> private String main_account;<br> private int type;<br> private String other_name;<br> private String password;<br> private int is_main_account;<br> private String money;<br> private int id;<br> public String getArea() {<br> return area;<br> }<br> public void setArea(String area) {<br> this.area = area;<br> }<br> public int getKind() {<br> return kind;<br> }<br> public void setKind(int kind) {<br> this.kind = kind;<br> }<br> public String getAccount() {<br> return account;<br> }<br> public void setAccount(String account) {<br> this.account = account;<br> }<br> public String getMain_account() {<br> return main_account;<br> }<br> public void setMain_account(String main_account) {<br> this.main_account = main_account;<br> }<br> public int getType() {<br> return type;<br> }<br> public void setType(int type) {<br> this.type = type;<br> }<br> public String getOther_name() {<br> return other_name;<br> }<br> public void setOther_name(String other_name) {<br> this.other_name = other_name;<br> }<br> public String getPassword() {<br> return password;<br> }<br> public void setPassword(String password) {<br> this.password = password;<br> }<br> public int getIs_main_account() {<br> return is_main_account;<br> }<br> public void setIs_main_account(int is_main_account) {<br> this.is_main_account = is_main_account;<br> }<br> public String getMoney() {<br> return money;<br> }<br> public void setMoney(String money) {<br> this.money = money;<br> }<br> public int getId() {<br> return id;<br> }<br> public void setId(int id) {<br> this.id = id;<br> }<br>}<br>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值