struts2迭代器的使用

1、前端jsp页面代码:

<s:iterator value="#request.specialityArray" status="status"  id="item">
        <tr>
            <td width="20%" align="center">
                    <s:property value="#status.count"/>
            td>
            <td width="60%" align="center">
                    <s:property value="SpecialityName"/>
            td>
            <td width="20%" align="center">
                <a href="Speciality.action?action=del&specialityid=SpecialityId"/>">
                    删除a>
            td>
        tr>
   s:iterator>

2、action后端代码:

String sql ="select * from Speciality";
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(sql);
ArrayList specialityArray=new ArrayList();
        while (rs.next()) {
            Speciality spec = new Speciality();
            spec.setSpecialityId(rs.getInt("specialityid"));
            spec.setSpecialityName(rs.getString("specialityname"));
            specialityArray.add(spec);
        }
        @SuppressWarnings("unchecked")
        Map> request = (Map>)
                ActionContext.getContext().get("request");
        request.put("specialityArray", specialityArray);
        DBConn.closeConn(conn);

特别注意:
1、

Map<String, ArrayList> request = (Map<String, ArrayList>)
                ActionContext.getContext().get("request");

这段代码是为了将map放入request范围内,在下面的代码中将map填充
2、在前端引用的类中的属性,一定需要getXXXX()来获取,否则前端无法获取到value中的值

你可能感兴趣的:(个人经验保存)