Struts2中iterator标签遍历map总结
针对以下几种情况的Map:
private Map<String,String> map;
private Map<String,Student> studentMap;
private Map<String,String[]> arrayMap;
private Map<String,List<Student>> listMap;
map=new HashMap<String,String>();
map.put("1", "one");
map.put("2", "two");
用法:
<s:iterator value="map" id="column">
<s:property value="#column"/><br>
key: <s:property value="key"/><br>
value:<s:property value="value"/><br>
</s:iterator>
studentMap=new HashMap<String,Student>();
studentMap.put("student1",new Student(new Long(1),"20034140201","张三1","男",25));
studentMap.put("student2",new Student(new Long(2),"20034140202","张三2","女",26));
studentMap.put("student3",new Student(new Long(3),"20034140202","张三3","男",27));
用法:
<s:iterator value="studentMap" id="column">
<tr>
<td><s:property value="#column"/></td>
<td><s:property value="value.id"/></td>
<td><s:property value="value.num"/></td>
<td><s:property value="value.name"/></td>
<td><s:property value="value.sex"/></td>
<td><s:property value="value.age"/></td>
</tr>
</s:iterator>
arrayMap= new HashMap<String,String[]>(); arrayMap.put( "arr1" , new String[]{ "1" , "2003401" , "leejie" , "male" , "20" }); arrayMap.put( "arr2" , new String[]{ "2" , "2003402" , "huanglie" , "male" , "25" }); arrayMap.put( "arr3" , new String[]{ "3" , "2003403" , "lixiaoning" , "male" , "21" });
用法
<s:iterator value= "arrayMap" id= "column" > <tr> <td><s:property value= "#column" /></td> <td><s:property value= "value[0]" /></td> <td><s:property value= "value[1]" /></td> <td><s:property value= "value[2]" /></td> <td><s:property value= "value[3]" /></td> <td><s:property value= "value[4]" /></td> </tr> </s:iterator>
listMap= new HashMap<String,List<Student>>(); List<Student> list1= new ArrayList<Student>(); list1.add( new Student( new Long( 1 ), "20034140201" , "张三1" , "男" , 25 )); list1.add( new Student( new Long( 2 ), "20034140202" , "张三2" , "男" , 25 )); list1.add( new Student( new Long( 3 ), "20034140203" , "张三3" , "男" , 25 )); listMap.put( "class1" , list1); List<Student> list2= new ArrayList<Student>(); list2.add( new Student( new Long( 1 ), "20034140301" , "李四1" , "男" , 20 )); list2.add( new Student( new Long( 2 ), "20034140302" , "李四2" , "男" , 21 )); list2.add( new Student( new Long( 3 ), "20034140303" , "李四3" , "男" , 22 )); list2.add( new Student( new Long( 4 ), "20034140304" , "李四4" , "男" , 23 )); listMap.put( "class2" , list2);
用法:
<s:iterator value= "listMap" id= "column" > <s:set name= "total" value= "#column.value.size" /> <s:iterator value= "#column.value" status= "s" > <tr> <s: if test= "#s.first" ><td rowspan= "${total}" ><s:property value= "#column.key" /></td></s: if > <td><s:property value= "id" /></td> <td><s:property value= "num" /></td> <td><s:property value= "name" /></td> <td><s:property value= "sex" /></td> <td><s:property value= "age" /></td> </tr> </s:iterator> </s:iterator>