使用Struts2标签遍历集合

时间:2021-10-24 14:02:57

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&lt;String,Objecg&gt;</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&lt;String,List&lt;Student&gt;&gt;</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&lt;Map&lt;String,Student&gt;&gt;</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标签遍历集合