`
碧血剑
  • 浏览: 212716 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts logic:itereate bean:write 标签

阅读更多
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>
分享到:
评论

相关推荐

    struts 标签 logic:iterate使用 logic:iterate

    我就是靠这个文档实现logic:iterate的循环的 struts 标签 logic:iterate使用 logic:iterate &lt;br&gt;第一页 是struts官方的说明, 第二页 是个例子 第三页 是我实现的arrayList放入标签的方法。 这是页面...

    struts1标签库

    struts标签库 文章目录 bean:cookie 2 bean:define 3 bean:header 4 bean:include 5 bean:message 5 bean:page 7 bean:parameter 7 bean:resource 8 bean:size 8 bean:struts 9 bean:write 9 html:base 10 ...

    logic:iterate标签当遍历的collection为Map时的使用

    iterate id="destMap" name="srcMap"&gt; &lt;br&gt; &lt;bean:define id="bean" name="destMap" property="value" /&gt; &lt;br&gt; &lt;bean:write name="bean" property="name" /&gt; &lt;br&gt;&lt;/logic:iterate&gt; &lt;br&gt;2. Map里存放的是...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 ...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、...

    李兴华 java_web开发实战经典 源码 完整版收集共享

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、...

    Java Oracle分页处理

    &lt;html:option value="pagetype" &gt;&lt;bean:write name="pagetype" /&gt;&lt;/html:option&gt; document.forms[0].page.options[${page.currentPage}-1].selected = true; &lt;/c:forEach&gt; ...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 下

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、...

    李兴华Java Web开发实战经典.pdf (高清版) Part1

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 ...

    李兴华 Java Web 开发实战经典 高清扫描版Part3

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、...

    java web 视频、电子书、源码(李兴华老师出版)

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 ...

    李兴华Java Web开发实战经典(高清版) Part2

    16.2.4、&lt;bean:write&gt;标签 16.2.5、&lt;bean:include&gt;标签 16.2.6、&lt;bean:resource&gt;标签 16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 ...

    ssh(structs,spring,hibernate)框架中的上传下载

    Struts+Spring+Hibernate实现上传下载    本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本:  •Struts 1.2  •Spring 1.2.5  •Hibernate 3.0  本文...

    外文翻译 stus MVC

    With Struts this is done with an Action class as a thin wrapper to the actual business logic. • Model state The model represents the state of the application. The business objects update the ...

Global site tag (gtag.js) - Google Analytics