logic:iterate

<logic:iterate>主要用来处理在页面上输出集合类,集合一般来说是下列之一:
1、 java对象的数组

2、 ArrayList、Vector、HashMap等

<logic:iterate>标记用于在页面中创建一个循环,以此来遍历如数组、Collection、Map这样的对象。该标记的功能强大,在Struts应用的页面中经常使用到。
1、对数组进行循环遍历
使用<logic:iterate>标记可以用于遍历数组,以下是一段示例代码:

<%
String[] testArray={"str1","str2","str3"}; 
pageContext.setAttribute("test",testArray); 
%>
<logic:iterate id="show" name="test"> 
<bean:write name="show"/> 
</logic:iterate>

在上面的代码中,首先定义了一个字符串数组,并为其初始化。接着,将该数组存入pageContext对象中,命名为test1。然后使用<logic:iterate>标记的name属性指定了该数组,并使用id来引用它,同时使用<bean:write>标记来将其显示出来。其结果为:
str1 
str2 
str3

另外,还可以通过length属性来指定输出元素的个数。如下面的代码:
<logic:iterate id="show" name="test" length="2" offset="1"> 
<bean:write name="show"/> 
</logic:iterate>

其中length属性指定了输出元素的个数,offset属性指定了从第几个元素开始输出,如此处为1,则表示从第二个元素开始输出。所以该代码的运行结果应当输出:
str2
str3

另外,该标记还有一个indexId属性,它指定一个变量存放当前集合中正被访问的元素的序号,如:

<logic:iterate id="show" name="test" length="2" offset="1" indexId="number"> 
<bean:write name="number"/>:<bean:write name="show"/> 
</logic:iterate>

其显示结果为:
1:str2 
2:str3 
2、对HashMap进行循环遍历

<%
HashMap countries=new HashMap();
countries.put("country1","中国");
countries.put("country2","美国");
countries.put("country3","英国");
countries.put("country4","法国");
countries.put("country5","德国");
pageContext.setAttribute("countries",countries); 
%>
<logic:iterate id="country" name="countries"> 
<bean:write name="country" property="key"/>:
<bean:write name="country" property="value"/> 
</logic:iterate>

在bean:write中通过property的key和value分别获得HaspMap对象的键和值。其显示结果为:
country5:德国 
country3:英国 
country2:美国 
country4:法国 
country1:中国 
由结果可看出,它并未按添加的顺序将其显示出来。这是因为HaspMap是无序存放的。

3、嵌套遍历

<%
String[] colors={"red","green","blue"};
String[] countries1={"中国","美国","法国"};
String[] persons={"乔丹","布什","克林顿"};
ArrayList list2=new ArrayList();
list2.add(colors);
list2.add(countries1);
list2.add(persons);
pageContext.setAttribute("list2",list2);
%>
<logic:iterate id="first" name="list2" indexId="numberfirst">
<bean:write name="numberfirst"/>
<logic:iterate id="second" name="first">
<bean:write name="second"/>
</logic:iterate>
<br>
</logic:iterate>

运行效果:
0 red green blue 
1 中国 美国 法国 
2 乔丹 布什 克林顿
<logic:iterate id="it" name="list" length="2"> 
<bean write name="it"/><br/> 
</logic:iterate>

list 是你要遍例的对象 it是list里面的元素的类型 ,还有个offset属性,是用来表示起始位置的,比如,offset=“1”从第二位开始遍历出值,offset=“0”是默认值

转自

logic:iterate(转)的更多相关文章

  1. 使用struts的logic:iterate标签遍历列表时得到显示序号

    <logic:notEmpty name="sList" scope="request"> <logic:iterate id="e ...

  2. struts标签<logic:iterate>的用法

    <logic:iterate>主要用来处理在页面上输出集合类,集合一般来说是下列之一: 1. java对象的数组 2. ArrayList.Vector.HashMap等 具体用法请参考s ...

  3. struts1吊牌&lt;logic:iterate&gt;

    <logic:iterate>主要用于处理网页上的输出集合,集合是其中一般下列之一: 1. java对象的数组 2. ArrayList.Vector.HashMap等 具体使用方法请參考 ...

  4. struts1 logic:iterate bean:write标签使用

    只是截取项目中部分代码,供参考及日后查阅 用struts1标签html:select 展现select下拉列表 刚开始为如下代码: <html:select name="Shuiwuj ...

  5. Struts标签<bean:write><logic:iterate></logic:equal>的组合使用小例

    form表单中的一个下拉列表控件的代码如下 <select name="taskname" id="taskname" class="selec ...

  6. 【转】Struts1.x系列教程(7):Logic标签库

    转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...

  7. logic标签用法

    logic  <logic:iterate> <% Dog dog1=new Dog(); dog1.setAge(2); dog1.setName("xiaoming& ...

  8. struts标签--logic总结

    1. logic:empty 该标签是用来判断是否为空的.如果为空,该标签体中嵌入的内容就会被处理.该标签用于以下情况: 1)当Java对象为null时: 2)当String对象为"&quo ...

  9. javaWeb中struts开发——Logic标签

    1.Struts标签的logic标签 Logic标签是逻辑标签,是Struts中比较重要的标签,完成各种逻辑运算操作,可以直接支持全局调转. 2.1<logic:present><l ...

随机推荐

  1. Java序列化对象-字符串转换

    package com.test; import com.alibaba.fastjson.JSON; import org.junit.Test; import java.io.ByteArrayI ...

  2. tensorflow 入门

    1.  tensorflow 官方文档中文版(下载) 2.  tensorflow mac安装参考 http://www.tuicool.com/articles/Fni2Yr 3. 源码例子目录 l ...

  3. [C基础修炼]如何用vs2017写一个C语言hello world程序

    从微软官网下载vs2017安装后,打开 文件>新建>项目>Visual C++空项目(选择名称,位置)确定>找到源文件 鼠标右击>添加>新建项>Visual ...

  4. [记录] CSS 多行文本超出部分省略

    如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览 <p style="width: 30 ...

  5. redis点

    (1)什么是redis? Redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) (2)Reids的特点 Redis本质上是一个Key-Value类型的 ...

  6. linux指令tar笔记

    tar 工具常用选项如表所列. 选项  说明 -c  创建存档文件,与-x相斥 -t  列出档案文件的文件列表 -x  解包存档文件,与-c相斥 -A  合并存档文件 -d  比较存档文件与源文件 - ...

  7. 机器学习入门-数值特征-数据四分位特征 1.quantile(用于求给定分数位的数值) 2.plt.axvline(用于画出竖线) 3.pd.pcut(对特征进行分位数切分,生成新的特征)

    函数说明: 1.  .quantile(cut_list) 对DataFrame类型直接使用,用于求出给定列表中分数的数值,这里用来求出4分位出的数值 2.  plt.axvline()  # 用于画 ...

  8. 递归获取包下的class文件

    ```java(这个居然隐藏不了) public class TestUrl { public static void main(String[] args) { String pageName = ...

  9. hadoop-1

    结合其他文章 http://weixiaolu.iteye.com/blog/1504898 https://www.cnblogs.com/dycg/p/3934394.html https://b ...

  10. python tuple排序

    tuple排序,按照索引0,1,2,3...依次比较 a = [3,1,0,9,6,2,4,8,7,5] a.sort(key = lambda x: (x%2, x, x%3)) # 先按照x%2的 ...