---
title: "struts2中iterator标签的相关使用"
author: "demojava"
date: "2010-08-18T18:13:12Z"
tags: []
categories: ["企业架构"]
slug: "struts2-iterator"
draft: false
lastmod: "2010-08-18T18:13:12Z"
id: 740421
url: "https://demojava.iteye.com/blog/740421"
---

在s:iterator标签的使用前，先了解下struts2中的Value Stack。这里参考了webwork中对Value Stack的描述，由于struts2是在webwork的基础上进行升级的，因此webwork对于Value Stack的表述同样适用于struts2。在这里不描述Value Stack具体做什么，但有两点需要注意：

 一个value stack本质上是一个List；  
在栈中调用\[n]将返回一个从位置n开始的子栈；  
对于2举个例子说明。假定Value Stack包含了\[model,action,others]，那么

\[0] --- 返回 \[model,action,others]；  
\[1] --- 返回 \[action,others]；  
\[2] --- 返回 \[others];  
现在将开始介绍s:iterator的一些使用。以下代码片段均在开发环境eclipse3.4 wtp、tomcat5.5、jdk5上使用struts2.1.6测试通过。

1\) 、访问 days

defined  List&lt;String&gt;  days   \["Monday","Thursday","Friday","Sunday"]

view plaincopy to clipboardprint?  
&lt;s:iterator value="days"&gt;&lt;s:property /&gt;&lt;/s:iterator&gt;   
&lt;s:iterator value="days"&gt;&lt;s:property /&gt;&lt;/s:iterator&gt;

2\) 、使用 top 关键字使用(过滤掉Monday)

defined  List&lt;String&gt;  days   \["Monday","Thursday","Friday","Sunday"]

view plaincopy to clipboardprint?  
&lt;s:iterator value="days"&gt;   
            &lt;s:if test="top!='Monday'"&gt;   
                &lt;s:property /&gt;   
            &lt;/s:if&gt;   
        &lt;/s:iterator&gt;   
&lt;s:iterator value="days"&gt;  
            &lt;s:if test="top!='Monday'"&gt;  
                &lt;s:property /&gt;  
            &lt;/s:if&gt;  
        &lt;/s:iterator&gt;

top 指代当前迭代元素，可以为对象；  
这里的top可用\[0].top替代，但不能使用\[0]。\[0]代表整个栈对象。如果单纯调用\[0]将会调用其toString()方法输出对象信息；

3)、使用 last / first 关键字使用

defined  String\[]\[] aTs = { { "一", "二", "三", "四" },{ "一一", "二二", "三三", "四四"} };

view plaincopy to clipboardprint?  
&lt;!--遍历二维数组，The trick here is to use 'top' as the value for the inner iterator--&gt;   
          &lt;s:iterator value="aTs" status="of"&gt;   
          &lt;s:if test="#of.last"&gt;&lt;br/&gt;&lt;/s:if&gt;   
          &lt;s:iterator value="top"&gt;   
&lt;!--亦可用\[0].top替代。如果单纯用\[0],则会同时打印该处栈对象信息--&gt;   
         &lt;s:property /&gt;   
          &lt;/s:iterator&gt;   
          &lt;/s:iterator&gt;   
&lt;!--遍历二维数组，The trick here is to use 'top' as the value for the inner iterator--&gt;  
          &lt;s:iterator value="aTs" status="of"&gt;  
       &lt;s:if test="#of.last"&gt;&lt;br/&gt;&lt;/s:if&gt;  
       &lt;s:iterator value="top"&gt;  
&lt;!--亦可用\[0].top替代。如果单纯用\[0],则会同时打印该处栈对象信息--&gt;  
   &lt;s:property /&gt;  
       &lt;/s:iterator&gt;  
          &lt;/s:iterator&gt;  

iterator 标签中的status属性代表当前迭代的位置；  
#of.last用于判断当前是否跌到的最后一个元素；  
last返回一个boolean类型;  
first 返回一个boolean类型;  
4)、使用 odd / even 关键字

下面的例子要实现每行输出颜色不同的效果。

defined  List&lt;String&gt;  days   \["Monday","Thursday","Friday","Sunday"]

view plaincopy to clipboardprint?  
       &lt;!--奇数行显示为红色,偶数行显示为绿色--&gt;   
       &lt;s:iterator value="days" status="offset"&gt;   
    &lt;s:else&gt;   
        &lt;s:if test="#offset.odd==true"&gt;   
            &lt;li style="color: red" mce\_style="color: red"&gt;&lt;s:property /&gt;&lt;/li&gt;   
        &lt;/s:if&gt;   
        &lt;s:else&gt;   
            &lt;li&gt;&lt;s:property /&gt;&lt;/li&gt;   
        &lt;/s:else&gt;   
    &lt;/s:else&gt;   
&lt;/s:iterator&gt;   
        &lt;!--奇数行显示为红色,偶数行显示为绿色--&gt;  
        &lt;s:iterator value="days" status="offset"&gt;  
  &lt;s:else&gt;  
   &lt;s:if test="#offset.odd==true"&gt;  
    &lt;li style="color: red" mce\_style="color: red"&gt;&lt;s:property /&gt;&lt;/li&gt;  
   &lt;/s:if&gt;  
   &lt;s:else&gt;  
    &lt;li&gt;&lt;s:property /&gt;&lt;/li&gt;  
   &lt;/s:else&gt;  
  &lt;/s:else&gt;  
 &lt;/s:iterator&gt;

odd关键字用来判断当前迭代位置是否为奇数行。odd返回boolean类型;  
evne关键字用来判断当前迭代位置是否为偶数行。even返回boolean类型  
5)、总结下，当声明iterator的status属性时，通过#statusName.method 可以使用以下方法:

even : boolean - 如果当前迭代位置是偶数返回true  
odd : boolean - 如果当前迭代位置是奇数返回true  
count : int - 返回当前迭代位置的计数(从1开始)  
index : int - 返回当前迭代位置的编号(从0开始)  
first : boolean - 如果当前迭代位置是第一位时返回true  
last : boolean - 如果当前迭代位置是最后一位时返回true  
modulus(operand : int) : int - 返回当前计数(从1开始)与指定操作数的模数  
6)、最后再来看下在iterator中调用value stack的用法。

假定countries是一个List对象，每一个country有一个name属性和一个citys List对象，并且每一个city也有一个name属性 。那么我们想要在迭代cities是访问countries的name属性就的用如下方式：

view plaincopy to clipboardprint?  
&lt;s:iterator value="countries"&gt;   
    &lt;s:iterator value="cities"&gt;   
        &lt;s:property value="name"/&gt;, &lt;s:property value="\[1].name"/&gt;&lt;br&gt;   
    &lt;/s:iterator&gt;   
&lt;/s:iterator&gt;   
           &lt;s:iterator value="countries"&gt;  
               &lt;s:iterator value="cities"&gt;  
                   &lt;s:property value="name"/&gt;, &lt;s:property value="\[1].name"/&gt;&lt;br&gt;  
               &lt;/s:iterator&gt;  
           &lt;/s:iterator&gt;

这里的 &lt;ww:property value="name"/&gt;取的是ctiy.name;&lt;ww:property value="\[1].name"/&gt;取得是country.name  
&lt;ww:property value="\[1].name"/&gt; 等价于 &lt;ww:property value="\[1].top.name"/&gt;  
we refer to a specific position on the stack: '\[1]'. The top of the stack, position 0, contains the current city, pushed on by the inner iterator; position 1 contains the current country, pushed there by the outer iterator.(city处于当前栈，即top或者\[0],而\[1]指明了外层iterator对象，即country)