logic:iterate(转)

logic:iterate

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

2、 ArrayList、Vector、HashMap等

 

<logic: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 乔丹 布什 克林顿
<logic:iterate id="it" name="list" length="2"> 
<bean write name="it"/><br/> 
</logic:iterate> 

list 是你要遍例的对象 it是list里面的元素的类型 ,还有个offset属性,是用来表示起始位置的,比如,offset=“1”从第二位开始遍历出值,offset=“0”是默认值

 

转自

转载于:https://www.cnblogs.com/YangBinChina/p/6399477.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我就是靠这个文档实现logic:iterate的循环的<br>struts 标签 logic:iterate使用 logic:iterate<br><br>第一页 是struts官方的说明,<br>第二页 是个例子<br>第三页 是我实现的arrayList放入标签的方法。<br>这是页面文件<br><%@ page language="java"<br> import="java.util.*,cn.edu.bit.zgc2d.accountQuery.*" pageEncoding="GBK"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"<br> prefix="bean"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-html"<br> prefix="html"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"<br> prefix="logic"%><br><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br><html><br> <head><br> </head><br> <body><br> <table><br> <tr><br> <td><%@ include file="../menu.txt"%><br> </td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=100 width=30><br> <img src=../images/small_2.gif><br> </td><br> <td><br> 账务查询<br> <br><br> 基本信息查询。<br> </td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=1 width=700 bgcolor=gray colspan=2></td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=100 width=30><br> <img src=../images/small_2.gif><br> </td><br> <td><br> 基本信息查询。<br> </td><br> </tr><br> </table><br> <table><br> <tr><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 帐号<br> </td><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 帐户别名<br> </td><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 是否为主帐户<br> </td><br> </tr><br> <logic:iterate id="item" name="list" indexId="index"><br> <tr><br> <td WIDTH=100 bgcolor=F2F2F2><br> <a href="www.sina.com.cn?abc=<bean:write name="item" property="account" />"><bean:write name="item" property="account" /><br> </td><br> <td WIDTH=100 bgcolor=F2F2F2><br> <bean:write name="item" property="other_name" /><br> </td><br> <td WIDTH=100 bgcolor=F2F2F2><br> <bean:write name="item" property="is_main_account" /><br> </td><br> </tr><br> </logic:iterate><br> </table><br> </body><br></html><br>这是action<br>public class InformationAction extends Action {<br> public ActionForward execute(ActionMapping mapping, ActionForm form,<br> HttpServletRequest request, HttpServletResponse response) {<br> InformationForm informationForm = (InformationForm) form;// TODO Auto-generated method stub<br> //业务开始<br> Connection conn = null; Statement st = null; ResultSet rs = null;<br> try {<br> db db = new db();<br> // 打开数据库<br> conn = db.conn();<br> // 创建st<br> st = conn.createStatement();<br> // 组织sql并执行<br> HttpSession session_account = request.getSession();<br> String account = (String) session_account.getAttribute("account");<br> String sql = "select * from account where main_account=" + account<br> + "order by is_main_account desc";<br> rs = st.executeQuery(sql);<br> // 组建arrayList列表<br> ArrayList list = new ArrayList();<br> while (rs.next()) {<br> accountBean bean = new accountBean();<br> bean.setArea(rs.getString("area")); bean.setKind(rs.getInt("kind")); bean.setAccount(rs.getString("account")); bean.setMain_account(rs.getString("main_account")); bean.setType(rs.getInt("type")); bean.setOther_name(rs.getString("other_name")); bean.setPassword(rs.getString("password")); bean.setIs_main_account(rs.getInt("is_main_account")); bean.setMoney(rs.getString("money")); bean.setId(rs.getInt("id"));<br> list.add(bean);<br> }<br> HttpSession session = request.getSession();<br> session.setAttribute("list", list);<br> return mapping.findForward("success");<br> } catch (Exception e) { e.printStackTrace(); } <br> //业务结束<br> return null;<br> }<br>}//自己将需要的包导入<br>bean文件<br>package cn.edu.bit.zgc2d.accountQuery;<br><br>public class accountBean {<br> private String area;<br> private int kind;<br> private String account;<br> private String main_account;<br> private int type;<br> private String other_name;<br> private String password;<br> private int is_main_account;<br> private String money;<br> private int id;<br> public String getArea() {<br> return area;<br> }<br> public void setArea(String area) {<br> this.area = area;<br> }<br> public int getKind() {<br> return kind;<br> }<br> public void setKind(int kind) {<br> this.kind = kind;<br> }<br> public String getAccount() {<br> return account;<br> }<br> public void setAccount(String account) {<br> this.account = account;<br> }<br> public String getMain_account() {<br> return main_account;<br> }<br> public void setMain_account(String main_account) {<br> this.main_account = main_account;<br> }<br> public int getType() {<br> return type;<br> }<br> public void setType(int type) {<br> this.type = type;<br> }<br> public String getOther_name() {<br> return other_name;<br> }<br> public void setOther_name(String other_name) {<br> this.other_name = other_name;<br> }<br> public String getPassword() {<br> return password;<br> }<br> public void setPassword(String password) {<br> this.password = password;<br> }<br> public int getIs_main_account() {<br> return is_main_account;<br> }<br> public void setIs_main_account(int is_main_account) {<br> this.is_main_account = is_main_account;<br> }<br> public String getMoney() {<br> return money;<br> }<br> public void setMoney(String money) {<br> this.money = money;<br> }<br> public int getId() {<br> return id;<br> }<br> public void setId(int id) {<br> this.id = id;<br> }<br>}<br>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值