`

struts2迭代器的用法

 
阅读更多

Name

Required

Default

Evaluated

Type

Description

 
begin false 0 false Integer if specified the iteration will start on that index
end false Size of the 'values' List or array, or 0 if 'step' is negative false Integer if specified the iteration will end on that index(inclusive)
id false   false String Deprecated. Use 'var' instead
status false false false Boolean If specified, an instanceof IteratorStatus will be pushed into stack upon each iteration
step false 1 false Integer if specified the iteration index will be increased by this value on each iteration. It can be a negative value, in which case 'begin' must be greater than 'end'
value false   false String the iteratable source to iterate over, else an the object itself will be put into a newly created List
var false   false String Name used to reference the value pushed into the Value Stack

Examples

The following example retrieves the value of the getDays() method of the current object on the value stack and uses it to iterate over. The <s:property/> tag prints out the current value of the iterator.

<s:iterator value="days">
  <p>day is: <s:property/></p>
</s:iterator>

The following example uses a Bean tag and places it into the ActionContext. The iterator tag will retrieve that object from the ActionContext and then calls its getDays() method as above. The status attribute is also used to create an IteratorStatus object, which in this example, its odd() method is used to alternate row colours:

<s:bean name="org.apache.struts2.example.IteratorExample" var="it">
  <s:param name="day" value="'foo'"/>
  <s:param name="day" value="'bar'"/>
</s:bean>
<p/>
<table border="0" cellspacing="0" cellpadding="1">
<tr>
  <th>Days of the week</th>
</tr>
<p/>
<s:iterator value="#it.days" status="rowstatus">
  <tr>
    <s:if test="#rowstatus.odd == true">
      <td style="background: grey"><s:property/></td>
    </s:if>
    <s:else>
      <td><s:property/></td>
    </s:else>
  </tr>
</s:iterator>
</table>

The next example will further demonstrate the use of the status attribute, using a DAO obtained from the action class through OGNL, iterating over groups and their users (in a security context). The last() method indicates if the current object is the last available in the iteration, and if not, we need to separate the users using a comma:

 <s:iterator value="groupDao.groups" status="groupStatus">
     <tr class="<s:if test="#groupStatus.odd == true ">odd</s:if><s:else>even</s:else>">
         <td><s:property value="name" /></td>
         <td><s:property value="description" /></td>
         <td>
             <s:iterator value="users" status="userStatus">
                 <s:property value="fullName" /><s:if test="!#userStatus.last">,</s:if>
             </s:iterator>
         </td>
     </tr>
 </s:iterator>

 

The next example iterates over a an action collection and passes every iterator value to another action. The trick here lies in the use of the '[0]' operator. It takes the current iterator value and passes it on to the edit action. Using the '[0]' operator has the same effect as using <s:property />. (The latter, however, does not work from inside the param tag).

     <s:action name="entries" var="entries"/>
     <s:iterator value="#entries.entries" >
         <s:property value="name" />
         <s:property />
         <s:push value="...">
             <s:action name="edit" var="edit" >
                 <s:param name="entry" value="[0]" />
             </s:action>
         </push>
     </s:iterator>

 

A loop that iterates 5 times

<s:iterator var="counter" begin="1" end="5" >
   <!-- current iteration value (1, ... 5) -->
   <s:property value="top" />
</s:iterator>

 

Another way to create a simple loop, similar to JSTL's <c:forEach begin="..." end="..." ...> is to use some OGNL magic, which provides some under-the-covers magic to make 0-n loops trivial. This example also loops five times.

<s:iterator status="stat" value="(5).{ #this }" >
   <s:property value="#stat.count" /> <!-- Note that "count" is 1-based, "index" is 0-based. -->
</s:iterator>

 

A loop that iterates over a partial list

<s:iterator value="{1,2,3,4,5}" begin="2" end="4" >
   <!-- current iteration value (2,3,4) -->
   <s:property value="top" />
</s:iterator>

分享到:
评论

相关推荐

    struts2 标签库 帮助文档

    Struts 2 标签库(文档手册) Tags-API-CLSW-JSP &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; 就能使用struts2.0的标签库 下面就介绍每个标签的具体应用实例说明:按字母排列 A: 1. 2. &lt;s:a href=""&gt;&lt;/s:a&gt;-...

    网上购物系统struts+Oracle10.2

    struts做的一个网上购物系统的练习,包括了以下几个方面的内容: 1.struts国际化处理, 2.动态DynaValidatorForm的用法, 3.tiles框架的使用, 4.Validator校验, ...7.iterate迭代器和JAVA泛型的用法

    java技术文档 常用类和经典应用

    迭代器Iterator的用法.pdf 反射机制.pdf 访问路经问题.pdf 基于MVC和RMI的分布.pdf 经典的查询方式.pdf 经典的显示方法.pdf 面向对象的实现.pdf 日期及其格式化.pdf 数字的格式化.pdf 网页之间共享数据.pdf 异常处理...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    UML和模式应用(架构师必备).part02.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    UML和模式应用(架构师必备).part01.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    UML和模式应用(架构师必备).part07.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    UML和模式应用(架构师必备).part06.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    UML和模式应用(架构师必备).part03.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    UML和模式应用(架构师必备).part04.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    UML和模式应用(架构师必备).part08.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    UML和模式应用(架构师必备).part05.rar

    6.21 过程:在迭代方法中如何使用用例 6.22 历史 6.23 参考资料 第7章 其他需求 7.1 如何完成这些示例 7.2 准则:初始阶段是否应该对此彻底地进行分析 7.3 准则:这些制品是否应该放在项目Web站点上 7.4 ...

    整理后java开发全套达内学习笔记(含练习)

    Iterator [java] 迭代器, 迭代程序 legal 合法的 ['li:gәl] log n.日志,记录 [lɒg] native (关键字) ?? ['neitiv] nested [java] 嵌套的 ['nestid] '如:内部类(nested classes) Object [java] 对象 ['ɒbdʒ...

    freemarker总结

    2,使用+运算符时,如果一边是数字,一边是字符串,就会自动将数字转换为字符串再连接,如:${3 + "5"},结果是:35 使用内建的int函数可对数值取整,如: ${ (x/2)?int } ${ 1.1?int } ${ 1.999?int } ${ -1.1?int } ...

    史上最全java面试,103项重点知识,带目录

    31. 迭代器 Iterator 是什么? 12 32. Iterator 怎么使用?有什么特点? 12 33. Iterator 和 ListIterator 有什么区别? 13 三、多线程 13 35. 并行和并发有什么区别? 13 36. 线程和进程的区别? 14 37. 守护线程是...

    Java常见面试题208道.docx

    31.迭代器 Iterator 是什么? 32.Iterator 怎么使用?有什么特点? 33.Iterator 和 ListIterator 有什么区别? 34.怎么确保一个集合不能被修改? 三、多线程 35.并行和并发有什么区别? 36.线程和进程的区别? 37....

    超级有影响力霸气的Java面试题大全文档

    finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。 16、sleep() 和 wait() 有什么区别? sleep是线程类(Thread)...

    java 面试题 总结

    finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等。 13、sleep() 和 wait() 有什么区别? sleep是线程类(Thread)的...

Global site tag (gtag.js) - Google Analytics