<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”是默认值
分享到:
相关推荐
struts标签使用中文说明文档 struts标签使用中文说明文档struts标签使用中文说明文档
struts标签使用例子,全面解释了struts1.2中的重要标签使用说明
详细解释并按照顺序将struts标签一一演示和使用的工程!
struts2标签使用例子 struts2标签使用例子 struts2标签使用例子struts2标签使用例子
struts标签的说明与使用,为方便广大程序员更熟练的掌握JSP页面标签的使用
struts标签 • Bean Tags:该标签库包含的标签可以用来创建bean、访问bean和访问bean的属性。同时提供了依据cookies、headers和parameters的值创建相关bean的能力。 HTML Tags:该标签库包含的标签可以用来创建...
struts标签库里面包括struts所有的标签使用源码
struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库struts标签库
Struts2标签库Struts2标签库Struts2标签库Struts2标签库Struts2标签库Struts2标签库
1.struts标签的使用 2.struts常用html标签库 3.在STRUTS中如何使用标签
struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签struts1标签
struts2标签库struts2标签库struts2标签库struts2标签库
Struts标签 Struts标签Struts标签Struts标签Struts标签
struts标签库,里面详细介绍了如何使用各种struts标签.
5.掌握常用标签textfield、radio、paaaword、checkboxlist、select、data的使用方法,实现页面与Action的交互操作 6.要求在创建过程中,包的名称要体现班级和学号,项目名称不需要按照实验手册中书写。在做的过程中...
Struts标签 大全
Struts提供了五个标签库,即:HTML、Bean、Logic、Template和Nested。 HTML 标签 : 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 ...Nested标签: 增强对其他的Struts 标签的嵌套使用的能力
struts 1.2 标签使用说明,简单介绍了struts1.2的标签使用
Struts 标签 页面跳转 数据库操作 Struts 标签 页面跳转 数据库操作 Struts 标签 页面跳转 数据库操作
包含了Struts2标签使用介绍的详细文档及APi文档,方便实用