Home >php教程 >PHP开发 >Usage of struts2's tag

Usage of struts2's tag

高洛峰
高洛峰Original
2016-12-13 17:41:451259browse

struts2's s:iterator can traverse any array, collection, etc. in the data stack. Here are some simple demos:


s:iterator tag has 3 attributes:
value: the collection being iterated
id: inside the specified collection The id of the element
Status Iterate the index of the element

1: jsp page definition element writing array or list

Html code

<s:iterator value="{&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;}" id=&#39;number&#39;>   
    <s:property value=&#39;number&#39;/>A   
</s:iterator>


The print result is: 1A2A3A4A5A

2: Index Usage


If status is specified, each iteration data has an instance of IteratorStatus, which has the following methods


int getCount() returns the number of elements currently iterated
int getIndex() returns the current element index
boolean isEven( ) Of course, whether the index is even
boolean isFirst() whether the current first element is
boolean isLast()
boolean isOdd() whether the current element index is odd

Html code

<s:iterator value="{&#39;a&#39;,&#39;b&#39;,&#39;c&#39;}" id=&#39;char&#39; status=&#39;st&#39;>   
    <s:if test="#st.Even">   
        现在的索引是奇数为:<s:property value=&#39;#st.index&#39;/>   
    </s:if>   
    当前元素值:<s:property value=&#39;char&#39;/>   
</s:iterator>

3: Traverse map


value can be directly defined as:

value="#{"1":"a","2":"b"}"
Each element is separated by . The key and value between elements are separated by colons. Value can also be the java.util.Map object in the data stack. The traversal is written as follows: Java's Object


3: Traverse the data stack. Simple List class:

Listb1c380fba13cd2af4696c1dd8ff44a0a

Java code

<s:iterator value="map" id="id" status="st">   
     key : <s:property value=&#39;key&#39;/>   
     value:<s:property vlaue=&#39;value&#39;/>   
</s:iterator>

Html code

class Attr{  
    String attrName;  
    String getAttrName(){  
         return "123";  
    }  
}

Of course value can also be written into value="% {label}" label can have. Operation

label's attribute List can be written as value="%{label.list}" which is equivalent to: getLabel().getList();


4: Traverse 2 lists:

Js code

<s:iterator value="label" id="id">   
    <s:property value="#id.attrName" />   
</s:iterator>


The elements of these two lists are one-to-one correspondence, one attrN corresponds to one attrV

Html code

List<AttrName> attrN {color,size,style}  
List<AttrValue> attrV {red,20,gay}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn