<logic:iterator>

原文地址:<logic:iterator> 作者:logmei

这东西是我一见钟情的,嘿嘿[转载]<logic:iterator>!一开始就引起了我的注意,每次见到它我都很激动,所以我要好好享受享受它了,把它弄的透透的,O(∩_∩)O哈哈~

<logic:iterate>是logic标签库中最复杂的标签,也是用途最广的标签,它能够在一个循环中遍历数组,Collection,Enumeration,Iterator或Map中的所有元素。(嘿嘿!不知道collection,Enumeration是什么,等会查查去)

1、遍历集合

<%

Vector animals=new Vector();

animals.addElement("dog");

animals.addElement("cat");

animals.addElement("bird");

animals.addElement("chick");

request.setAttribute("Animals",animals);

%>

<logic:iterate id="element" name="Animals">

     <bean:write name="element"/>

</logic:iterate>

以上代码先定义一个vector类型的集合变量Animals,它存放在request范围内,接下来<logic:iterate>在一个循环中遍历Animals集合中的所有元素,每次检索到一个元素,就把它命名为"element",并存放在page范围内。<bean:write name="element"/>用于输出每个元素的内容。

<logic:iterate id="element" name="Animals" IndexId="index" offset="1" length="2">

     <bean:write name="index"/><bean:write name="element"/><br>

</logic:iterate>

length属性指定需要遍历的元素的数目,如果没有设置length属性,就遍历集合中的所有元素,offset属性指定开始遍历的起始属性,默认值是"0",表示从集合的第一的元素开始。indexId属性定义代表当前遍历元素序号的变量,这个变量被存放在page范围内,可以被标签主体的<bean:write>标签访问。

2、遍历Map

<%

   HashMap months=new HashMap();

   months.put("Jan.","January");

   months.put("Feb.","February");

   months.put("Mar","March");

   request.setAttribute("Months",months);

%>

<logic:iterate id="element" indexId="ind" name="Months">

    <bean:write name="ind"/>.<bean:write name="element" property="key"/>:

    <bean:write name="element" property="value"/><br>

</logic:iterate>

 先定义一个名为"months"的HashMap,存放在request范围内,<logic:iterate>遍历months的每个元素,每个元素都包含一对key/value。

如果HashMap中的每个元素的value是集合对象,则可以采用嵌套的<logic:iterate>遍历集合中的所有对象:

<%

   HashMap h=new HashMap();

   String vegetables[]={"pepper","cucumber"};

   String fruits[]={"apple","orange","banana","cherry"};

   String flowers[]={"chrysanthemum","rose"};

   String trees[]={"willow"};

   h.put("Vegetables",vegetables);

   h.put("Fruits",fruits);

   h.put("Flowers",flowers);

   h.put("Trees",trees);

   request.setAttribute("catalog",h);

%>

<logic:iterate id="element" name="catalog" indexId="ind">

   <bean:write name="ind">.<bean:write name="element" property="key"><br>

   <logic:iterate id="elementValue" name="element" property="value" lenth="3" offset="1">

       ------<bean:write name="elementValue"/><br>

   </logic:iterate>

</logic:iterate>

3、设置被遍历的变量

可以通过以下方式来设置需要遍历的变量。

1)设置name属性,name属性指定需要遍历的集合或Map

    <logic:iterate id="element" name="Animals">

       <bean:write name="element">

    </logic:iterate>

2)设置name属性和property属性,name属性指定一个javaBean,property指定一个javaBean的一个属性,这个属性为需要变量的集合和Map

    <logic:iterate id="element" indexId="ind" name="Catalog">

       <bean:write name="ind"/>.<bean:write name="element" property="key"/>:

       <bean:iterate id="elementValue" name="element" property="value" length="3" offset="1">

           ------<bean:write name="elementValue"/><br>

        </bean:iterate>

    </logic:iterate>

 3)设置collection属性,collection属性指定一个运行时表达式,表达式的运算结果为需要遍历的集合或Map

<logic:iterate id="header" collection="<%=request.getHeaderNames()%>">

     <bean:write name="header"/>

</logic:iterate>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值