使用struts2中,使用Iterator标签可以遍历任何可以遍历的类型,这里就不多做实例了,简单的写几个常用的,初次使用常出错的
一共写了两个实例;Map和数组
一: 先看map遍历
(1) MapAction.java
package cn.intertor.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import cn.entity.Student; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class MapAction extends ActionSupport { private Map<String, String> map; // string集合 private Map<String, Student> studentMap; // Student对象集合 private Map<String, String[]> arrayMap; // 数组集合 private Map<String, List<Student>> listMap; // list集合 public String testMap() { map = new HashMap<String, String>(); map.put("1", "one"); map.put("2", "two"); studentMap = new HashMap<String, Student>(); studentMap.put("student1", new Student(new Long(1), "20034140201", "小奎", "男", 19)); studentMap.put("student2", new Student(new Long(2), "20034140202", "小灿", "女", 19)); studentMap.put("student3", new Student(new Long(3), "20034140203", "小君", "男", 18)); 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" }); listMap = new HashMap<String, List<Student>>(); List<Student> list1 = new ArrayList<Student>(); list1.add(new Student(new Long(1), "20034140201", "小奎", "男", 19)); list1.add(new Student(new Long(2), "20034140202", "小灿", "女", 19)); list1.add(new Student(new Long(3), "20034140203", "小君", "男", 18)); listMap.put("class1", list1); List<Student> list2 = new ArrayList<Student>(); list2.add(new Student(new Long(1), "20034150201", "李四1", "男", 20)); list2.add(new Student(new Long(2), "20034150201", "李四2", "男", 21)); list2.add(new Student(new Long(3), "20034150201", "李四3", "男", 22)); list2.add(new Student(new Long(4), "20034150201", "李四4", "男", 23)); listMap.put("class2", list2); return SUCCESS; } /* 省略get和set方法 */ }
(2)Student实体类
package cn.entity; public class Student { private long id; private String num; private String name; private int age; private String sex; public Student() { } public Student(Long id, String num, String name, String sex, int age) { this.id = id; this.num = num; this.name = name; this.sex = sex; this.age = age; } /* 省略get和set方法 */ }
(3)Struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="map" class="cn.intertor.demo.MapAction" method="testMap"> <result>testMap.jsp</result> </action> </package> </struts>
(4) testMap.jsp
<%@ page contentType="text/html;charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>struts2中的map遍历总结</title> </head> <body> <b>1.map中的value为String字符串</b> <br> <s:iterator value="map" var="column"> <s:property value="#column" /> <br> key: <s:property value="key" /> <br> value:<s:property value="value" /> <br> ******************************************<br> </s:iterator> <b>2.map中的value为Student对象</b> <table border="1" width="50%" cellspacing="0" cellpadding="0"> <tr> <td>key=value</td> <td>ID</td> <td>num</td> <td>name</td> <td>sex</td> <td>age</td> </tr> <s:iterator value="studentMap" var="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> </table> <p> <b>3.map中的value为String数组</b> <table border="1" width="50%" cellspacing="0" cellpadding="0"> <tr> <td>key=value</td> <td>ID</td> <td>num</td> <td>name</td> <td>sex</td> <td>age</td> </tr> <s:iterator value="arrayMap" var="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> </table> <p> <b>4.map中的value为list集合</b> < table border="1" width="50%" cellspacing="0" cellpadding="0"> <tr> <td>class</td> <td>ID</td> <td>num</td> <td>name</td> <td>sex</td> <td>age</td> </tr> <s:iterator value="listMap" var="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> </table> </body> </html>
结果:
第二个例子,定义一个jsp页面
<%@ page pageEncoding=“GBK " language="java" import="java.util.*"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>遍历小例子</title> </head> <body> (1)遍历int数组****************************************** <br /> <s:iterator value="{1,2,3,4,5,6}"> <s:property /> <br /> </s:iterator> (2)遍历String数组************************************** <br /> <s:iterator value="{'aaa','bbb','ccc','ddd','eee','fff','ggg'}" var="s" status="status"> 转换大写:<s:property value="#s.toUpperCase()" /> 索引:<s:property value="#status.count" /> 是否奇数:<s:property value="#status.even" /> 是否偶数:<s:property value="#status.odd" /> 是否第一个元素:<s:property value="#status.first" /> 是否最后一个元素<s:property value="#status.last" /> <br /> </s:iterator> (3)变量Map集合_(1):***************************************** <br /> <s:iterator value="#{1:'小奎',2:'小灿',3:'嘿嘿'}"> <s:property value="key" /> | <s:property value="value" /> <br /> </s:iterator> (4)变量Map集合_(2):***************************************** <br /> <s:iterator value="#{1:'小奎',2:'小灿',3:'嘿嘿'}" var="m"> <s:property value="#m.key" /> | <s:property value="#m.value" /> <br /> </s:iterator> </body> </html>
结果:
未经允许请勿转载:程序喵 » 【SSH】Struts2标签Iterator遍历数组集合总结