使用Struts2标签遍历集合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Struts2标签遍历集合相关的知识,希望对你有一定的参考价值。
Student.java
package action; public class Student { private Long sid; private String name; public Student() { // TODO Auto-generated constructor stub } public Student(Long sid,String name) { this.sid=sid; this.name=name; } public Long getSid() { return sid; } public void setSid(Long sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
ClassHelloAction.java
package action; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;public class ClassHelloAction extends AbstractAjaxAction { private String message; private List<String> names; private List<Student> stus; private Map<String, Object> map; private Map<String,List<Student>> stuMaps; private List<Map<String,Student>> listMap; public List<Map<String, Student>> getListMap() { return listMap; } public void setListMap(List<Map<String, Student>> listMap) { this.listMap = listMap; } public Map<String, List<Student>> getStuMaps() { return stuMaps; } public void setStuMaps(Map<String, List<Student>> stuMaps) { this.stuMaps = stuMaps; } public List<Student> getStus() { return stus; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public void setStus(List<Student> stus) { this.stus = stus; } public List<String> getNames() { return names; } public void setNames(List<String> names) { this.names = names; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute() throws Exception { this.stuMaps=new HashMap<String, List<Student>>(); List<Student> list1=new ArrayList<Student>(); list1.add(new Student(1L,"aa")); list1.add(new Student(2L,"bb")); list1.add(new Student(3L,"cc")); list1.add(new Student(4L,"dd")); List<Student> list2=new ArrayList<Student>(); list2.add(new Student(1L,"qq")); list2.add(new Student(2L,"ww")); list2.add(new Student(3L,"ee")); list2.add(new Student(4L,"rr")); //List<Student> stus=new ArrayList<Student>(); stus.addAll(list1); stus.addAll(list2); //Map<String,Object> this.map=new HashMap<String, Object>(); map.put("1", "广州"); map.put("2", "珠海"); map.put("3", "东莞"); map.put("4", "佛山"); //Map<string,List<Student>> stuMaps.put("一班", list1); stuMaps.put("二班", list2); //List<Map<String,Student>> this.listMap=new ArrayList<Map<String,Student>>(); Map<String,Student> map1=new HashMap<String, Student>(); map1.put("1号", new Student(1L,"鸣人")); map1.put("2号", new Student(2L,"佐助")); Map<String,Student> map2=new HashMap<String, Student>(); map1.put("3号", new Student(3L,"路飞")); map1.put("4号", new Student(4L,"索隆")); listMap.add(map1); listMap.add(map2); return "success"; } }
<div style="float: left; border:thin solid;" >
<p>遍历Map<String,Objecg></p>
<ul>
<s:iterator value="map" id="id">
<li><s:property value="#id.key" />:<s:property
value="#id.value" /></li>
</s:iterator>
</ul>
</div>
<div style="float: left;border: thin solid;">
<p>遍历Map<String,List<Student>></p>
<ul>
<s:iterator value="stuMaps" id="id" status="st"><li><s:property value="#id.key" />: <s:select list="#id.value"
id="stu" listKey="sid" listValue="name"></s:select></li></s:iterator>
</ul>
</div>
<div style="float: left;border:thin solid;">
<p>遍历List<Map<String,Student>></p>
<ul>
<s:iterator value="listMap" id="map" status="st">
<s:iterator value="#map" id="mk" status="sa">
<li><s:property value="#mk.key" />:<s:property
value="#mk.value.name" /></li>
</s:iterator>
</s:iterator>
</ul>
</div>
4、index.jsp页面效果:
以上是关于使用Struts2标签遍历集合的主要内容,如果未能解决你的问题,请参考以下文章
[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段
struts2的action中有两个list集合,如何在页面中取得
用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,