• Strutes2标签iterator


      iterator标签是用月迭代一个集合,这里的集合可以是Collection、Map、Enumeration、Iterator或者array数组。iterator标签在迭代过程中,会把迭代的每一个对象暂时压入值栈,这样在标签的内部就可以直接访问对象的属性和方法,在标签执行完毕后,位于栈顶的对象就会被删除,在第二次迭代过程中,再压入新的对象。

      iterator标签的属性如下表所示(The begin, end and step attributes are only available from 2.1.7 on): 

    Dynamic Attributes Allowed:false

    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

    注:如果为status属性指定一个值,那么将创建一个org.apache.struts2.views.jsp.IteratorStatus实例。

    IteratorStatus类有以下的方法:

    public int getCount()
    得到当前迭代的元素的总数
    
    public int getIndex()
    得到当前迭代的元素的索引
    
    public boolean isEven()
    判断当前迭代的元素的顺序是否是偶数
    
    public boolean isOdd()
    判断当前迭代的元素的顺序是否是奇数
    
    public boolean isFirst()
    判断当前迭代的元素的顺序是否是第一个元素
    
    public boolean isLast()
    判断当前迭代的元素的顺序是否是最后一个元素

    IteratorStatus 类的这些方法分别对应了count、index、even、odd、first、last属性。

    示例:

    1,迭代列表

    <s:iterator value="#{'a','b','c'}" status="status">
      <s:property/><br>  
      <s:property value="#status.count"/><br>
      <s:property value="#status.index"/><br>
      ... </s:iterator>

    <s:iterator value="campList" id="campMap" status="status">
      <s:property value="#status.index +1"/>
    </s:iterator>

    2,迭代Map

    <s:iterator value="#{'a':'aa','b':'bb','c':'cc'}">
        <s:property value="key"/><br>
        <s:property value="value"/>
    </s:iterator>

      在上述代码中,我们使用OGNL表达式#{'a':'aa','b':'bb','c':'cc'}创建了一个包含三个元素的Map对象。在迭代Map时,实际上迭代的是Map的entrySet()方法返回的集合,在这个集合中是一组Map.Entry对象,可以利用Map.Entry对象的getKey()和getValue()方法来获取对应的键值对信息。

     3,begin和end使用

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


    参考:

    Apache Struts 2 Documentation

    http://struts.apache.org/docs/iterator.html

  • 相关阅读:
    文件的复制
    反射基础知识
    蓝牙连接 返回的命令
    WebRoot 与 webContent的区别
    时间判断
    java ecplise配置
    异常org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 11; 注释中不允许出现字符串 "--"。的原因
    F、CSL 的神奇序列 【规律】 (“新智认知”杯上海高校程序设计竞赛暨第十七届上海大学程序设计春季联赛)
    E、CSL 的魔法 【模拟】 (“新智认知”杯上海高校程序设计竞赛暨第十七届上海大学程序设计春季联赛)
    D、CSL 的字符串 【栈+贪心】 (“新智认知”杯上海高校程序设计竞赛暨第十七届上海大学程序设计春季联赛)
  • 原文地址:https://www.cnblogs.com/lemon-now/p/5109768.html
Copyright © 2020-2023  润新知