Struts2标签—Iterator标签

来源:互联网 发布:网络摄像头平台 编辑:程序博客网 时间:2024/05/18 01:50

Iterator标签

该标签用于对集合、Map和数组进行迭代。

迭代LIst:

public String testList(){Person person1 = new Person();person1.setName("张三");person1.setAge(23);Person person2 = new Person();person2.setName("李四");person2.setAge(24);Person person3 = new Person();person3.setName("狗蛋");person3.setAge(25);List<Person> list = new ArrayList<Person>();list.add(person1);list.add(person2);list.add(person3);//将list存放到对象栈ActionContext.getContext().getValueStack().push(list);//将list存放到Map栈ActionContext.getContext().put("personList", list);return "iterator";}
迭代方式:

<span style="white-space:pre"></span><!-- 迭代对象栈的List --><table><tr><td>姓名</td><td>年龄</td></tr><s:iterator><tr><td><s:property value="name"/></td><td><s:property value="age"/></td></tr></s:iterator></table><!-- 迭代对象栈中的List --><table> <tr><td>姓名</td><td>年龄</td></tr><s:iterator value="#personList"><tr><td><s:property value="name"/></td><td><s:property value="age"/></td></tr></s:iterator></table>
——————————————————————————————————————————————————————————

迭代Map

<span style="white-space:pre"></span>public String testMap(){Person person1 = new Person();person1.setName("张三");person1.setAge(23);Person person2 = new Person();person2.setName("李四");person2.setAge(24);Person person3 = new Person();person3.setName("狗蛋");person3.setAge(25);Map<String, Person> map = new HashMap<String, Person>();map.put("person1", person1);map.put("person2", person2);map.put("person3", person3);//将Map存放到对象栈ActionContext.getContext().getValueStack().push(map);//将Map存放到Map栈ActionContext.getContext().put("map", map);return "iterator";}
迭代方式:

<span style="white-space:pre"></span><!-- 迭代对象栈中的map --><table> <tr><td>姓名</td><td>年龄</td></tr><s:iterator ><tr><td><s:property value="value.name"/></td><td><s:property value="value.age"/></td></tr></s:iterator></table><!-- 迭代Map栈的中Map --><table> <tr><td>姓名</td><td>年龄</td></tr><s:iterator value="#map"><tr><td><s:property value="value.name"/></td><td><s:property value="value.age"/></td></tr></s:iterator></table>
总结:

1、value属性不写,默认迭代栈顶的元素。

2、value属性指向要迭代的List、Map或数组。

3、当前迭代的元素在栈顶。

4、var指向当前正在迭代的元素,在map栈中

5、status属性表示正在迭代的状态:

int getCount() 返回当前迭代的元素个数
int getIndex() 返回当前迭代元素的索引
boolean isEven() 返回当前迭代元素的索引是否是偶数
boolean isOdd() 返回当前迭代元素的索引是否是奇数
boolean isFirst() 返回当前迭代元素是否为第一个元素
boolean isLast() 返回当前迭代元素是否为最后一个元素




0 0
原创粉丝点击