`

在struts中用actionForm传递数组

阅读更多

我在action中从数据库取得了所有空间数据的表名,并存入了数组tableName,想用actionForm直接传递到jsp页面上。经过我一下午的努力,终于实现了一个简单的例子。

    1、tableNameForm的定义:

public class tableNameForm extends ActionForm {
    private Integer count;  //表的个数
    private String[] name;  //表名数组
   
    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public void setName(String[] name) {
        this.name = name;
    }

    public String[] getName() {
        return name;
    }

    public ActionErrors validate(ActionMapping actionMapping,
                                 HttpServletRequest httpServletRequest) {
            /** @todo: finish this method, this is just the skeleton.*/
        return null;
    }

    public void reset(ActionMapping actionMapping,
                      HttpServletRequest servletRequest) {
    }
}

    2、然后在action中,要用setName()对form赋值:

……

String[] tablename = new String[columnCount];  //用数组存放空间数据表的表名,这里的columnCount在此之前从数据库查出来的,表示表的个数

for (int i = 0; i < columnCount; i++) //数组初始化
   tablename[i] = "";

……

while(rs.next()){
   tablename[i++] = rs.getString("TABLENAME").trim();
}

……

tableNameForm.setName(tablename);

……

    3、最后,在jsp页面中显示的时候用到了两个taglib:bean和logic,具体代码断为:

……

        <logic:present name="tableNameForm" property="name">
        空间数据表列:<br />
          <logic:iterate id="tablename" indexId="ind" name="tableNameForm" property="name">
            <bean:write name="ind"/>.<bean:write name="tablename"/><br />
          </logic:iterate>
        </logic:present>
……

    总结:先在form中建立数组属性及其相应的get和set方法,然后在action中对其用set进行赋值,最后就是在jsp中显示出来。<logic:iterate>中的id指定了<bean:write>中要输出的内容,所以这个必须要和<bean:write>中的name属性一致;indexId指定迭代的序号,表示当前是第几条记录;name指定包含了数组的form的名字;property指定需要迭代的form中的数组属性名。

    刚才的jsp片断显示出来的页面效果就是: 

    空间数据表列:

    0.GDS_LANDMK

    1.GDS_ROAD

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics