您的位置:首页 > 编程语言 > Java开发

struts 怎么使用 s标签 迭代 map

2013-05-22 16:42 471 查看
package com.shuangXing.testDemo;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

public class MapAction extends ActionSupport   

{   

  

    private Map<String,String> map;   

      

    private Map<String,Student> studentMap;   

       

    private Map<String,String[]> arrayMap;   

       

    private Map<String,List<Student>> listMap;   

       

  

  

    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","张三1","男",25));   

         studentMap.put("student2",new Student(new Long(2),"20034140202","张三2","女",26));   

         studentMap.put("student3",new Student(new Long(3),"20034140202","张三3","男",27));   

           

         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","张三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);   

            

           

           

           

        return SUCCESS;   

           

     }   

  

       

       

    public Map<String, String> getMap() {   

        return map;   

     }   

  

    public void setMap(Map<String, String> map) {   

        this.map = map;   

     }   

       

    public Map<String, Student> getStudentMap() {   

        return studentMap;   

     }   

  

    public void setStudentMap(Map<String, Student> studentMap) {   

        this.studentMap = studentMap;   

     }   

  

  

    public Map<String, String[]> getArrayMap() {   

        return arrayMap;   

     }   

  

  

    public void setArrayMap(Map<String, String[]> arrayMap) {   

        this.arrayMap = arrayMap;   

     }   

  

  

  

    public Map<String, List<Student>> getListMap() {   

        return listMap;   

     }   

  

  

  

    public void setListMap(Map<String, List<Student>> listMap) {   

        this.listMap = listMap;   

     }   

       
       

}  

<%@ 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" id="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" 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>   

   </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" 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>   

   </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" 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>   

</table>   

     

     

</body>   

</html>  

package com.shuangXing.testDemo;

public class Student {

long id;
String num;
String name;
String sex;
int age;

public Student(long id, String num, String name, String sex, int age) {
super();
this.id = id;
this.num = num;
this.name = name;
this.sex = sex;
this.age = age;
}
 
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNum() {
ret
c452
urn num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

//运行结果*********************************************************************

1.map中的value为String字符串
2=two
key: 2
value:two
******************************************
1=one
key: 1
value:one
******************************************
2.map中的value为Student对象

key=valueIDnumnamesexage
student2=com.shuangXing.testDemo.Student@25c78000220034140202张三226
student1=com.shuangXing.testDemo.Student@4ca49360120034140201张三125
student3=com.shuangXing.testDemo.Student@48c7aaef320034140202张三327
3.map中的value为String数组
key=valueIDnumnamesexage
arr3=[Ljava.lang.String;@230a476132003403lixiaoningmale21
arr2=[Ljava.lang.String;@77d4433f22003402huangliemale25
arr1=[Ljava.lang.String;@108b1a8f12003401leejiemale20
4.map中的value为list集合
classIDnumnamesexage
class1120034140201张三125
220034140202张三225
320034140203张三325
class2120034140301李四120
220034140302李四221
320034140303李四322
420034140304李四423
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: