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="{'1','2','3','4','5'}" id='number'> <s:property value='number'/>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="{'a','b','c'}" id='char' status='st'> <s:if test="#st.Even"> 现在的索引是奇数为:<s:property value='#st.index'/> </s:if> 当前元素值:<s:property value='char'/> </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='key'/> value:<s:property vlaue='value'/> </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();
Js code
<s:iterator value="label" id="id"> <s:property value="#id.attrName" /> </s:iterator>
List<AttrName> attrN {color,size,style} List<AttrValue> attrV {red,20,gay}