1. If there is such an Action:
Java code
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Import com. opensymphony. xwork2.ActionSupport
Import com. model. Student
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", "James 1", "male", 25 ));
StudentMap. put ("student2", new Student (new Long (2), "20034140202", "Zhang San 2", "female", 26 ));
StudentMap. put ("student3", new Student (new Long (3), "20034140202", "James 3", "male", 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", "James 1", "male", 25 ));
List1.add (new Student (new Long (2), "20034140202", "Zhang San 2", "male", 25 ));
List1.add (new Student (new Long (3), "20034140203", "James 3", "male", 25 ));
ListMap. put ("class1", list1 );
List <Student> list2 = new ArrayList <Student> (); www.2cto.com
List2.add (new Student (new Long (1), "20034140301", "Li Si 1", "male", 20 ));
List2.add (new Student (new Long (2), "20034140302", "Li Si 2", "male", 21 ));
List2.add (new Student (new Long (3), "20034140303", "Li Si 3", "male", 22 ));
List2.add (new Student (new Long (4), "20034140304", "Li Si 4", "male", 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;
}
}
This action provides various types of map. Below we have a jsp, we want to display these maps to this jsp:
Jsp code
<% @ Page contentType = "text/html; charset = UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<Html>
<Head>
<Title> Summary of map traversal in struts2 </title>
</Head>
<Body>
<B> 1. The value in map is a 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. The value in map is a Student object </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. The value in map is a String array </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. The value in map is a list collection. </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>
<1. <s: iterator value = "listHashMap" id = "listid">
<S: iterator value = "# listid. value" id = "listidsub">
<Tr>
<Td> <s: property value = "key"/> </td>
<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>