博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
s:iterator用法
阅读量:6360 次
发布时间:2019-06-23

本文共 5433 字,大约阅读时间需要 18 分钟。

hot3.png

struts2
遍历map小结1.MapAction.java 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
map; private Map
studentMap; private Map
arrayMap; private Map
> listMap; public String testMap() { map=new HashMap
(); map.put("1", "one"); map.put("2", "two"); studentMap=new HashMap
(); 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
(); 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
>(); List
list1=new ArrayList
(); 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
list2=new ArrayList
(); 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
getMap() { return map; } public void setMap(Map
map) { this.map = map; } public Map
getStudentMap() { return studentMap; } public void setStudentMap(Map
studentMap) { this.studentMap = studentMap; } public Map
getArrayMap() { return arrayMap; } public void setArrayMap(Map
arrayMap) { this.arrayMap = arrayMap; } public Map
> getListMap() { return listMap; } public void setListMap(Map
> listMap) { this.listMap = listMap; } } 2.testMap.jsp <%@ page contentType="text/html;charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %>
struts2中的map遍历总结
1.map中的value为String字符串
key:
value:
******************************************
2.map中的value为Student对象
key=value ID num name sex age

3.map中的value为String数组

key=value ID num name sex age

4.map中的value为list集合

<1.
class ID num name sex age
-------------------------------------------------------------------------------------------------------------------------------------------------4、普通属性 public String execute(){ username = "zhangsan"; password = "123"; }
5、自定义对象 public String execute(){ userInfo = new UserInfo(); userInfo.setUsername("hhh"); userInfo.setPassword("111"); }
6、List(普通属性) public String execute(){ list = new ArrayList
(); list.add("aaa"); list.add("bbb"); list.add("ccc"); }
或者:
6、List(自定义对象属性) public String execute(){ list = new ArrayList
(); UserInfo userInfo = new UserInfo(); userInfo.setUsername("u1"); userInfo.setPassword("p1"); userInfo.setRoleInfo(new RoleInfo("r1")); list.add(userInfo); userInfo = new UserInfo(); userInfo.setUsername("u2"); userInfo.setPassword("p2"); userInfo.setRoleInfo(new RoleInfo("r2")); list.add(userInfo); }
或者:
7、数组(普通属性) public String execute(){ strs = new String[2]; strs[0] = "as"; strs[1] = "asd"; }
或者:
8、数组(自定义对象属性) public String execute(){ strs = new UserInfo[2]; UserInfo userInfo = new UserInfo(); userInfo.setUsername("u11"); userInfo.setPassword("11"); strs[0] = userInfo; userInfo = new UserInfo(); userInfo.setUsername("u22"); userInfo.setPassword("22"); strs[1] = userInfo; }
或者:
8、数组(自定义对象属性) public String execute(){ strs = new UserInfo[2]; UserInfo userInfo = new UserInfo(); userInfo.setUsername("u11"); userInfo.setPassword("11"); userInfo.setRoleInfo(new RoleInfo("r11")); strs[0] = userInfo; userInfo = new UserInfo(); userInfo.setUsername("u22"); userInfo.setPassword("22"); userInfo.setRoleInfo(new RoleInfo("r22")); strs[1] = userInfo; }
或者:
8、Map(普通属性) public String execute(){ map = new HashMap
(); map.put("k1", "v1"); map.put("k2", "v2"); }
或者:
8、Map(自定义对象属性) public String execute(){ map = new HashMap
(); UserInfo userInfo = new UserInfo(); userInfo.setUsername("u1"); userInfo.setPassword("p1"); userInfo.setRoleInfo(new RoleInfo("r1")); map.put("k1", userInfo); userInfo = new UserInfo(); userInfo.setUsername("u2"); userInfo.setPassword("p2"); userInfo.setRoleInfo(new RoleInfo("r2")); map.put("k2", userInfo); }
或者
8、Map嵌套Map(自定义对象属性) public String execute(){ map = new TreeMap
>(); Map
innerMap = new TreeMap
(); UserInfo userInfo = new UserInfo(); userInfo.setUsername("u1"); userInfo.setPassword("p1"); userInfo.setRoleInfo(new RoleInfo("r1")); userInfo = new UserInfo(); userInfo.setUsername("u11"); userInfo.setPassword("p11"); userInfo.setRoleInfo(new RoleInfo("r11")); innerMap.put("k1", userInfo); innerMap.put("k11", userInfo); map.put("key1", innerMap); // innerMap = new TreeMap
(); userInfo = new UserInfo(); userInfo.setUsername("u2"); userInfo.setPassword("p2"); userInfo.setRoleInfo(new RoleInfo("r2")); userInfo = new UserInfo(); userInfo.setUsername("u22"); userInfo.setPassword("p22"); userInfo.setRoleInfo(new RoleInfo("r22")); innerMap.put("k2", userInfo); innerMap.put("k22", userInfo); map.put("key2", innerMap); }

 

转载于:https://my.oschina.net/Skynet01/blog/1163009

你可能感兴趣的文章