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

2、 ArrayList、Vector、HashMap等

具体用法请参考struts文档,这里不作详细介绍

现在定义一个class,User.java 把它编译成User.class

package example;

import java.io.Serializable; 
public final class User implements Serializable { 
private String name = null; 
private String password = null;

public String getName () { 
return (this.name); 
}

public void setName(String name) { 
this.name = name; 
}

public String getPassword () { 
return (this. password); 
}

public void setPassword (String password) { 
this. password = password; 
}

}

然后在一个struts webapplication中创建一个jsp,例如iterate.jsp

<%@ page language="java" %> 
<%@ page import="example.*"%> 
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<% 
java.util.ArrayList list = new java.util.ArrayList(); 
User usera=new User(); 
usera.setName("white"); 
usera.setPassword("abcd"); 
list.add(usera); 
User userb=new User(); 
userb.setName("mary"); 
userb.setPassword("hijk"); 
list.add(userb); 
session.setAttribute("list", list);

%>

<html><body><table width="100%">

<logic:iterate id="a" name="list" type=" example.User ">

<tr><td width="50%">

name: <bean:write name="a" property="name"/>

<td/><td width="50%">

password: <bean:write name="a" property="password"/>

</td></tr>

</logic:iterate>

</table></body></html> 
将User.class, iterate.jsp放到相应的目录,运行iterate.jsp你就可以看到iterate的效果了

iterate标记 
id 脚本变量的名称,它保存着集合中当前元素的句柄。 
name 代表了你需要叠代的集合,来自session或者request的属性。 
type 是其中的集合类元素的类型

bean 的write标记是用来将属性输出的,name用来匹配iterate的id,property用来匹配相应类的属 性<logic:iterate>用法详解22007-04-04 20:34<login: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 乔丹 布什 克林顿  [/size][/size][/color][/color]

struts标签<logic:iterate>的用法的更多相关文章

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

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

  2. struts标签--logic总结

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

  3. logic:iterate(转)

    logic:iterate struts标签<logic:iterate>的用法 StrutsBeanJSPWeb脚本  <logic:iterate>主要用来处理在页面上输出 ...

  4. 转载-struts中logic标签使用

    Struts中Logic逻辑标签的作用及用法 Struts中Logic逻辑标签的作用及用法 2006年10月18日 星期三 21:34 Terry原创,转载请说明作者及出处 Logic标签大部分的功能 ...

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

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

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

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

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

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

  8. Struts的Logic标签的用途

    Struts的Logic标签可以根据特定的逻辑条件来判断网页的内容,或者循环遍历集合元素,它和HTML,Bean标签是Struts应用中最常用的三个标签. 它的功能主要是比较运算,进行字符串的匹配,判 ...

  9. 基本STRUTS标签-学习笔记-Logic标签

    BEAN标签(name 是从别处得来的:id是自己的,相当于变量:property相当于变量的值) 前提: String str=request.getParameter("param&qu ...

随机推荐

  1. struts2将servlet对象注入到Action中

    在struts2框架中,可以通过IoC方式将servlet对象注入到Action中,通常需要Action实现以下接口: a. ServletRequestAware: 实现该接口的Action可以直接 ...

  2. postgresql 函数&存储过程 ; 递归查询

    函数:http://my.oschina.net/Kenyon/blog/108303 紧接上述,补充一下: 输入/输出参数的函数demo(输入作为变量影响sql结果,输出作为结果返回) create ...

  3. ndk学习20: jni之OnLoad动态注册函数

    一.原理 当在系统中调用System.loadLibrary函数时,该函数会找到对应的动态库, 然后首先试图找到"JNI_OnLoad"函数,如果该函数存在,则调用它 JNI_On ...

  4. scp和rsync限制传输速度

    1.scp 限速100KB/s scp -l 1000 test root@192.168.1.104 此时的传输速率就是1M/8=100KB左右 2.rsync 限速100KB/s rsync -a ...

  5. ubuntu14.04 us sources.list

    deb http://us.archive.ubuntu.com/ubuntu/ trusty main restricted deb-src http://us.archive.ubuntu.com ...

  6. Read N Characters Given Read4 I & II

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  7. POJ 1002

    #include <stdio.h> #include <string.h> #include <stdlib.h> struct In{ int a; ]; }p ...

  8. wxPython中文教程入门实例

    这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下     wxPython中文教程入门实例 wx.Window 是一个基类 ...

  9. js中修改标签的hidden属性

    hidden属性在html5中,只要存在,就是隐藏效果,而不论值为多少 要显示元素,要删除hidden属性,而不是设置为false <script type="text/javascr ...

  10. 多线程同步_Monitor

    多线程一直在学习和理解中...... Monitor类是多线程中用以实现同步的一种技术,主要是同一进程内多线程间的同步技术. Monitor类中有以下几个方法需要注意: Monitor.Enter(o ...