struts标签iterate的例子

    在逻辑标记库中定义了<logic:iterate>标记,它能够根据特定集合中元素的数目对标记体的内容进行重复的检查。集合的类型可以是java.util.Iterator,java.util.Collection , java.util.Map或是一个数组。
有三种方法可以定义这个集合:
1.使用运行时间表达式来返回一个属性集合的集合
2.将集合定义为bean,并且使用name属性指定存储属性的名称。
3.使用name属性定义一个bean,并且使用property属性定义一个返回集合的bean属性。
当前元素的集合会被定义为一个页作用域的bean。属性如下,所有这些属性都能使用运行时表达式。

属性描述
collection如果没有设置name属性,它就指定了要进行重复的集合
Id页作用域bean和脚本变量的名称,它保存着集合中当前元素的句柄
indexed 页作用域JSP bean的名称,它包含着每次重复完成后集合的当前索引
Length 重复的最大次数
Name作为集合的bean的名称,或是一个bean名称,它由property属性定义的属性,是个集合
Offset 重复开始位置的索引
property 作为集合的Bean属性的名称
Scope如果指定了bean名称,这个属性设置bean的作用域。若没有设置,搜索范围从页到应用程序作用域
Type 为当前定义的页作用域bean的类型

以下是运用了HashMap和Vector的iterate标签的例子:

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Vector" %>
<html:html>
<head>
<title>Logic Iterate sample code</title>
</head>
<body bgcolor="white">

<h3>Logic Iterate sample code</h3>

<p>This page provides examples of the following Struts LOGIC tags:<br>

<ul>
<li>&lt;logic:iterate&gt;</li>
</ul>

<table border="1">
<tr>
<td>
<%--
Variables used on this page
--%>

<%
HashMap months = new HashMap();
months.put("Jan.", "January");
months.put("Feb.", "February");
months.put("Mar.", "March");
request.setAttribute("months", months);
%>
<%--
The following section shows iterate.
--%>
<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><P>

</td>

<td>
<%
HashMap h = new HashMap();
String vegetables[] = {"pepper", "cucumber"};
String fruits[] = {"apple","orange","banana","cherry","watermelon"};
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);
%>
<%--
The following section shows iterate.
--%>

<logic:iterate id="element" indexId="ind" name="catalog">
  <bean:write name="ind"/>. <bean:write name="element" property="key"/><BR>
  <logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
      -----<bean:write name="elementValue"/><BR>
  </logic:iterate>
</logic:iterate><P>

</td>
<td>
<%
 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"/><BR>
</logic:iterate><p>

</td>
<td>
<logic:iterate id="element" indexId="index" name="Animals" offset="1" length="3">
   <bean:write name="index"/>.<bean:write name="element"/><BR>
</logic:iterate><p>
</td>
<td>
<logic:iterate id="header" collection="<%= request.getHeaderNames() %>">
   <bean:write name="header"/><BR>
</logic:iterate>
</td>
</tr>
<table>
</body>
</html:html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值