当前位置: 代码迷 >> J2EE >> Session中有一个HashMap 集合,怎么使用struts自带标签输出集合中的每一个属性!
  详细解决方案

Session中有一个HashMap 集合,怎么使用struts自带标签输出集合中的每一个属性!

热度:446   发布时间:2016-04-22 02:25:50.0
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
  相关解决方案