struts1吊牌<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 约旦 灌木 克林顿
版权声明:本文博客原创文章,博客,未经同意,不得转载。
struts1吊牌<logic:iterate>的更多相关文章
- struts1 logic:iterate bean:write标签使用
只是截取项目中部分代码,供参考及日后查阅 用struts1标签html:select 展现select下拉列表 刚开始为如下代码: <html:select name="Shuiwuj ...
- 使用struts的logic:iterate标签遍历列表时得到显示序号
<logic:notEmpty name="sList" scope="request"> <logic:iterate id="e ...
- struts标签<logic:iterate>的用法
<logic:iterate>主要用来处理在页面上输出集合类,集合一般来说是下列之一: 1. java对象的数组 2. ArrayList.Vector.HashMap等 具体用法请参考s ...
- Struts标签<bean:write><logic:iterate></logic:equal>的组合使用小例
form表单中的一个下拉列表控件的代码如下 <select name="taskname" id="taskname" class="selec ...
- logic:iterate(转)
logic:iterate struts标签<logic:iterate>的用法 StrutsBeanJSPWeb脚本 <logic:iterate>主要用来处理在页面上输出 ...
- vue实现带logo的二维码/商品条形码/打印商品吊牌
一.带logo的二维码 1.安装 npm install vue-qr --save 2.在页面或组件中使用 <template> <div id="qrcode" ...
- struts 2吊牌s:if 、s:iterator注意
疏忽,也没有相应的总结.实际上JSTL标签Struts2标签混淆.导致一些上述问题的细节.今天我给从下一个总结,同 后不要再犯这种错误. 总喜欢在s:if标签里面使用$,导致各种数据读不出来. str ...
- 【转】Struts1.x系列教程(7):Logic标签库
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...
- struts1四:常用标签
struts1支持的5种标签: HTML 标签: 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 Bean 标签: 在访问JavaBeans 及其属性,以及定义一个 ...
随机推荐
- _beginThreadex创建多线程解读
_beginThreadex创建多线程解读 一.须要的头文件支持 #include <process.h> // for _beginthread() 须要的设置:Proj ...
- Leetcode_191_Number of 1 Bits
本文是在学习中的总结.欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44486547 Write a function that ...
- hdu1005 Number Sequence(寻找循环节)
主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...
- Web服务器Raspkate的RESTful API
基于轻量型Web服务器Raspkate的RESTful API的实现 在上一篇文章中,我们已经了解了Raspkate这一轻量型Web服务器,今天,我们再一起了解下如何基于Raspkate实现简单的RE ...
- Follow your hear(跟着心走)
端午三天的哈尔滨之旅已经over,非常开心真的非常开心.听了刘四风老师的"为爱开讲.我爱这世界"的论坛,尽管.这三天老师讲的不多.可是句句是精华.Follow your heart ...
- FPGA开机状态
最近调试FPGA电路时发现一个问题,我从来没有注意过.我们都知道Xilinx的FPGA有三种功率M引脚,这是为了让我们配置三个引脚FPGA装载机模式,什么是主要的字符串.从字符串.并行等.,该手册有. ...
- Linux System Programming note 8 ——File and Directory Management
1. The Stat Family #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> ...
- telnet模拟http訪问
HTTP协议经常使用的无非就那么几个命令 GET HEAD PUT POST 此处简单说下http的GET和HEAD 举两个简单的样例: GET的样例 telnet serverip 80 GET h ...
- cocos2d-x于android在call to OpenGL ES API with no current context
一.问题: 正在使用JNI离Java(Android)侧 打回来C++(Cocos2d-x)该函数返回消息.Cocos2d-x花掉了 看看 Eclipse的Log中.显示 有 call to Open ...
- Pagination jquery ajax 分页参考资料
http://www.zhangxinxu.com/wordpress/2010/01/jquery-pagination-ajax%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB% ...