使用Struts2标签遍历集合
- 遍歷Map<String,Object>
- 遍歷Map<Stirng,List<Student>>
- 遍歷List<Map<String,Student>>
?
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";}}
?
?
1、遍歷Map<String,Object>
<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>
2、遍歷Map<String,List<Student>>
<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>
3、遍歷List<Map<String,Student>>
<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頁面效果:
?
轉載于:https://www.cnblogs.com/gongchenglion/p/struts2.html
總結
以上是生活随笔為你收集整理的使用Struts2标签遍历集合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 是男人就下100层【第五层】——2048
- 下一篇: 辇图是谁画的呢?