`

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标签使用例子

    Struts2是一个强大的MVC(模型-视图-控制器)框架,用于构建企业级Java Web应用程序。在Struts2框架中,标签库是其一大特色,它提供了丰富的自定义标签,使得开发者能够更加便捷地创建动态页面。这些标签极大地简化...

    迭代器的使用

    Struts2作为一款流行的MVC框架,其OGNL(Object-Graph Navigation Language)表达式语言提供了一种灵活的方式来访问和操作对象,包括使用迭代器来遍历数据。 1. **Struts2中的`s:iterator`标签** Struts2的`s:...

    struts2的复习

    2. MVC架构:Struts2遵循MVC设计模式,将应用程序分为模型、视图和控制器三个部分,帮助开发者分离业务逻辑与表现层,提高代码的可维护性和可复用性。 三、Action类与结果 3. Action类:Action类是业务逻辑的载体,...

    Struts2标签使用及API文档

    `struts2标签用法.doc`和`标签详解`文件可能包含更深入的标签用法和实例,例如如何使用`s:form`创建表单,如何利用`s:action`执行Action,以及如何使用`s:debug`标签进行调试等。 5. **Struts2标签的高级用法** -...

    Struts2案例翻译篇-Using Struts2 Tag

    Struts2是一个强大的Java web框架,它提供了丰富的功能来构建MVC(模型-视图-控制器)架构的应用程序。在Struts2中,Tag库是其核心组件之一,极大地简化了视图层的开发,使得开发者可以更方便地创建动态网页。本篇将...

    Struts2知识点总结

    Struts2是Apache软件基金会旗下一个开源的MVC(模型-视图-控制器)框架,它用于开发Java Web应用。它的前身是著名的WebWork框架,并且与Struts1没有直接关系,尽管它们的命名相似。Struts2最初基于WebWork的核心...

    struts2API文档

    **Struts2.chm文档**可能是官方API的离线帮助文件,这种文件通常包含了类库的所有类、接口、方法和属性的详细说明,是开发者查阅API时的重要参考。在这个文档中,你可以找到关于Action、Result、Interceptor等核心...

    轻松入门之struts2 3.0版

    Struts2是一个强大的Java web应用程序框架,用于构建和管理...通过阅读“轻松入门之Struts2 3.0版”,开发者不仅能掌握Struts2的基本用法,还能了解到其与其他流行框架的集成方法,从而能够更高效地开发Java web应用。

    Struts2图书馆管理系统

    此外,Struts2的拦截器(Interceptors)也扮演着重要角色,它们可以实现全局的功能,如权限验证、日志记录、异常处理等。通过配置struts.xml,开发者可以灵活定制拦截器链,实现业务逻辑的模块化。 在"Library"这个...

    自己写的 Struts 2源代码 例子(包括Struts2 标签)

    本压缩包包含的源代码例子旨在帮助理解Struts 2的工作原理和使用方式,特别是Struts 2的标签库。 首先,我们来了解一下Struts 2的核心概念: 1. **Action类**:是业务逻辑的载体,通常继承自`org.struts.action....

    Struts2学习笔记

    Struts框架的演化经历了几个关键阶段,从最初的Struts1.x,到后来的WebWork,再到现在的Struts2.x,每一次迭代都带来了显著的技术革新和性能提升。 - **Struts1.x**:基于Servlet API,使用`ActionServlet`作为核心...

    Struts2开发jar包

    Struts2是一款强大的Java web框架,它为开发者提供了一种结构化的、可扩展的方式来构建MVC(模型-视图-控制器)模式的应用程序。在Java世界中,Struts2是许多企业级应用首选的框架之一,因为它简化了复杂的web开发...

    taglib(struts2标签).doc

    可以使用`count`属性来限制生成的迭代器中的元素数量。例如: ```jsp &lt;!-- 生成一个简单的 iterator --&gt; ,bbb,ccc,ddd,eee'}"&gt; &lt;s:property /&gt;&lt;br/&gt; &lt;!-- 生成包含前三个元素的 iterator --&gt; ,bbb,ccc,...

    struts2最新jar包

    2. **拦截器(Interceptor)**:Struts2的一大特色是使用拦截器来处理请求。拦截器是一系列按照特定顺序执行的函数,可以实现如日志、权限验证、事务管理等通用功能,无需在每个Action中重复编写。 3. **配置文件**...

    struts2源码最新

    2. **拦截器(Interceptors)**:Struts2的一个强大特点是使用拦截器来处理请求。拦截器链允许开发者在Action执行前后插入自定义的处理逻辑,如日志、权限检查、数据验证等。 3. **插件体系**:Struts2支持丰富的...

    struts2遍历集合

    根据提供的文件信息,本文将详细解析Struts2框架中遍历集合的方法,并附带介绍一些常用的Struts2标签使用技巧。 ### Struts2框架简介 Struts2是Apache基金会下的一个开源项目,它是一个用于开发Java Web应用程序的...

    Struts2.x 项目实战

    配置方面,Struts2.x使用XML配置文件(struts.xml或struts-default.xml)来定义Action、结果类型、拦截器等,同时支持注解方式配置,提高开发效率。此外,Struts2.x还支持动态方法调用,使得Action方法的调用更加...

    struts2基本标签用法

    本文将详细解析Struts2标签的基本用法,涵盖从变量设置到表单处理等多个方面。 ### 1. 变量与表达式 在Struts2中,可以使用`&lt;s:set&gt;`标签来设定变量。例如: ```xml ``` 这里的`name`属性指定了变量名,`value`...

    struts2标签库

    这些标签通常配合Struts2的拦截器和结果类型一起使用,可以实现数据验证、文件上传、权限控制等功能。例如,`&lt;s:fielderror&gt;`标签用于显示验证错误信息,`&lt;s:actionerror&gt;`则显示全局错误消息。 在实际应用中,...

Global site tag (gtag.js) - Google Analytics