转载(http://www.java3z.com/cwbwebhome/article/article1a/171.html?id=364)
在逻辑标记库中定义了<logic:iterate>标记,它能够根据特定集合中元素的数目对标记体的内容进行重复的检查。集合的类型可以是java.util.Iterator,java.util.Collection,java.util.Map
或是一个数组,也可以用name属性定义一个bean,并且使用property属性指定要循环的bean的属性(这个属性返回集合)类型。
下面请看struts1.2.4中自带的例子:
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html>
<head>
<title>测试struts-logic Iterate 标记</title>
</head>
<body bgcolor="white">
<%
{
java.util.ArrayList list = new java.util.ArrayList();
list.add("First");
list.add("Second");
list.add("Third");
list.add("Fourth");
list.add("Fifth");
pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);
int intArray[] = new int[]
{ 0, 10, 20, 30, 40 };
pageContext.setAttribute("intArray", intArray, PageContext.PAGE_SCOPE);
}
%>
<div align="center">
<h1>测试struts-logic Iterate标记</h1>
</div>
<jsp:useBean id="bean" scope="page" class="org.apache.struts.webapp.exercise.TestBean" />
<jsp:useBean id="list" scope="page" class="java.util.ArrayList" />
<h3>测试1 - 从头到尾叠代(循环)一个字符串数组[0..4]</h3>
<ul>
<!--用name属性指定一个bean,property属性指定要循环的bean的属性,id集合中当前元素的句柄,index1保存当前索引值-->
<logic:iterate id="element" name="bean" property="stringArray" indexId="index1">
<li>
<em>
<bean:write name="element" />
</em> [
<bean:write name="index1" />]</li>
</logic:iterate>
</ul>
<h3>测试2 - 叠代一个字符串数组(循环三次)Array [0..2]</h3>
<ul>
<logic:iterate id="element" name="bean" property="stringArray" indexId="index" length="3">
<li>
<em>
<bean:write name="element" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ul>
<h3>测试3 - 叠代一个String Array [3..4](输出索引从3开始的元素)</h3>
<ul>
<logic:iterate id="element" name="bean" property="stringArray" indexId="index" offset="3">
<li>
<em>
<bean:write name="element" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ul>
<h3>测试4 - 叠代一个String Array [1..3]</h3>
<ul>
<logic:iterate id="element" name="bean" property="stringArray" indexId="index" offset="1" length="3">
<li>
<em>
<bean:write name="element" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ul>
<h3>测试5 - 从头至尾叠代一个Array List</h3>
<ul>
<logic:iterate id="item" name="list" indexId="index">
<li>
<em>
<bean:write name="item" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ul>
<h3>测试6 - 叠代一个Array List [0..2]</h3>
<ol>
<logic:iterate id="item" name="list" indexId="index" offset="0" length="3">
<li>
<em>
<bean:write name="item" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ol>
<h3>测试7 - 叠代一个Array List [2..4]</h3>
<ol>
<logic:iterate id="item" name="list" indexId="index" offset="2" length="3">
<li>
<em>
<bean:write name="item" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ol>
<h3>测试8 - 叠代一个int array</h3>
<ul>
<logic:iterate id="item" name="intArray" indexId="index">
<li>
<em>
<bean:write name="item" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ul>
<h3>测试 9 - 叠代一个int array [0..2]</h3>
<ul>
<logic:iterate id="item" name="intArray" indexId="index" length="3">
<li>
<em>
<bean:write name="item" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ul>
<h3>测试10 - 叠代一个int array [2..4]</h3>
<ul>
<logic:iterate id="item" name="intArray" indexId="index" offset="2" length="3">
<li>
<em>
<bean:write name="item" />
</em> [
<bean:write name="index" />]</li>
</logic:iterate>
</ul>
</body>
</html>
下面是TestBean.java的部分:
package org.apache.struts.webapp.exercise;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;
public class TestBean extends ActionForm {
//省略
private String stringArray[] =
{ "String 0", "String 1", "String 2", "String 3", "String 4" };
public String[] getStringArray() {
return (this.stringArray);
}
public void setStringArray(String stringArray[]) {
this.stringArray = stringArray;
}
//省略..
}
posted on 2008-08-21 10:13
Hank1026 阅读(1018)
评论(0) 编辑 收藏 所属分类:
每日积累