读书人

Session中有一个HashMap 集合,怎么使用

发布时间: 2012-03-05 11:54:02 作者: rapoo

Session中有一个HashMap 集合,如何使用struts自带标签输出集合中的每一个属性!!!!!!!!
Session中有一个HashMap 集合,集合中都是User类的vo对象,
在jsp中用 struts 自带<logic:iterate >如何获得对象中的属性

例如User类:userid,username,password.等3个属性,
User user1=new User("a","aaa","aaa");
User user2=new User("b","bbb","bbbb");
User user3=new User("c","bbcsdf","sdf");
HashMap中:
hash.put(user1.userid,user1);
hash.put(user2.userid,user2);
hash.put(user3.userid,user3);

我如何才能分别输出他们的属性? 请大虾指教,不知道可不可以输出!!!!!!!!!!!!!!!!!!!!!


[解决办法]
在<logic:iterate>内遍历hashmap取得的vo用<bean:write>输出属性的时候如下来写:

<logic:iterate id="id" name="Form名字" property="hashmap名字">
...
<bean:write name="id" property="value.属性名"/>

加的value即代表你从hashmap中取得的vo
[解决办法]
============== action ===============
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
UserForm user1=new UserForm();
user1.setUserid("a");
user1.setUsername("aaaa");
user1.setPassword("aaaa");
UserForm user2=new UserForm();
user2.setUserid("b");
user2.setUsername("bbbb");
user2.setPassword("bbbb");
UserForm user3=new UserForm();
user3.setUserid("c");
user3.setUsername("cccc");
user3.setPassword("cccc");
List<UserForm> list1 = new ArrayList<UserForm>();
List<UserForm> list2 = new ArrayList<UserForm>();
List<UserForm> list3 = new ArrayList<UserForm>();
list1.add(user1);
list2.add(user2);
list3.add(user3);
HashMap<String, List<UserForm>> map = new HashMap<String, List<UserForm>>();
map.put(user1.getUserid(),list1);
map.put(user2.getUserid(),list2);
map.put(user3.getUserid(),list3);

request.getSession().setAttribute("userAllMap", map);
return mapping.findForward("ok");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("fail");
}

=========== jsp ===========
<table align="center">
<tr>
<td>学号</td>
<td>姓名</td>
<td>密码</td>
</tr>
<logic:iterate id="user" name="userAllMap">
<tr>
<logic:iterate name="user" property="value" id="userValue" >
<td>
<bean:write property="userid" name="userValue" />
</td>
<td>
<bean:write property="username" name="userValue" />
</td>
<td>
<bean:write property="password" name="userValue" />
</td>
</logic:iterate>
</tr>
</logic:iterate>
</table>

struts 迭代输出 map 集合方法:

1.map集合的 value 是 字符对象
String str ="str123";
HashMap<String, String> map = new HashMap<String, String>();
<logic:iterate id="str" name="map">
<bean:write name="str" property="key"/>//输出key
<bean:write name="str" property="value"/>//输出value


</logic:iterate>

2.map集合的 value 是 集合
就点用嵌套迭代

String[] a={"a" ,"ab","abc"};
String[] b={"1" ,"12","123","dfd"};
Strign[] c={"s"}
HashMap<String, String[]> map = new HashMap<String, String[]>();
map.put("a",a);
map.put("b",b);
map.put("c",c);
<logic:iterate id="strValue" name="map">
<bean:write name="strValue" property="key"/>>//输出key
/************* 对 value 进行迭代输出 **********/
<logic:iterate id="strValue" name="strValue" property="value">
<bean:write name="strValue" />//如果是集合装的是对象 就加上property 属性
</logic:iterate>
</logic:iterate>

注意: hash.put(user1.userid,user1); 值是user对象 这是跌倒不出来的
会提示 Cannot create iterator for this collection 错误
以为logic:iterate 标签的 name 属性只能放 集合或字符对象 不能是实体对象
============================= 个人意见 ================================
如果没有特殊需求建议直接用list集合 不要要用map了

[解决办法]
如果是单个对象不用嵌套 就像GODProbe 说的
<logic:iterate id="user" name="userAllMap">
<tr>
<td>
<bean:write name="user" property="key"/>//键
</td>
<td>
<bean:write name="user" property="value.userid"/>
</td>
<td>
<bean:write name="user" property="value.username"/>
</td>
<td>
<bean:write name="user" property="value.password"/>
</td>
</tr>
</logic:iterate>

读书人网 >J2EE开发

热点推荐