从jsp到action类获取List对象的值 [英] Get the List object value from a jsp to action class

查看:357
本文介绍了从jsp到action类获取List对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSP中的迭代列表对象,其值来自显示正确的ViewAction类.

Iterating List object in a JSP whose value coming from ViewAction class which is displaying proper.

下面是jps代码.

<s:iterator value="beanList" status="stat">
    <tr> 
         <td>    
             <input type="checkbox" name="subCheckBox" />
         </td>   
         <td>                 
             <s:textfield name="beanList[%{#stat.index}].rollnumber" 
                          value="%{rollnumber}" theme="simple"/>
         </td>
         <td>
             <s:textfield name="beanList[%{#stat.index}].name" 
                          value="%{name}" theme="simple"/>
         </td>
         <td>
             <s:textfield name="beanList[%{#stat.index}].location" 
                          value="%{location}" theme="simple"/>
         </td> 
    </tr>     
</s:iterator>

ViewAction.java和Bean类代码如下

ViewAction.java and Bean class code is as follows

在操作类列表中,对象名称为 beanList

In the action class list object name is beanList

public class ViewCheckboxAction extends ActionSupport  {
    HttpServletRequest request = ServletActionContext.getRequest();
    String viewData = "select * from student order by rollno";
    List<Bean> beanList;

    public List<Bean> getBeanList() {
        return beanList;
    }  

    public void setBeanList(ArrayList<Bean> beanList) {
        this.beanList = beanList;
    }

    public String execute() {
        beanList = new ArrayList<Bean>();
        DbConnection db = new DbConnection();
        int counter = 0;
        try {
            Statement st = db.getConnection().createStatement();
            ResultSet res = st.executeQuery(viewData);
            while(res.next()) {
                  counter++;
                  Bean bean = new Bean(res.getInt(1),
                                       res.getString(2),
                                       res.getString(3));
                  rollNumber.add(res.getString("rollno"));
                  beanList.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { 
                db.removeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(counter>0)
           return SUCCESS;
        else 
           return ERROR;
    }   
}

Bean:

public class Bean {
    int rollnumber;
    String name;
    String location;

    public Bean(int x, String y, String z) {
        rollnumber = x;
        name = y;
        location = z;
    }

    getters and setters...

我需要从jsp到操作的多个/单个更新的表单字段值 类以执行更新的操作.但是列表(beanList) 值在操作类中为空.由于它无效,因此我无法进行更新 手术. 1)在新的动作类(EditAction.java)中,如何初始化列表对象( beanList )? 这与我在ViewAction.java中声明的方式相同 2)Jsp sysntax是否正确? 要求您提供帮助.预先感谢.

I need the multiple/single updated form field value from jsp to action class in order to do the updated operation. But the list(beanList) value is nullified in the action class. Since it is nullified i can not do the update operation. 1)In the new action class(EditAction.java) how to initialise the list object(beanList) ? It is the same way as i declare in ViewAction.java 2)Is the Jsp sysntax is proper ? Request you to help on this. Thanks in advance.

推荐答案

向您的Bean类添加默认的无参数构造函数.

之所以这样称呼默认的无参数构造函数,是因为它是默认的:如果不指定 any 构造函数,它将是自动创建的.

The default no-args Constructor is called like that because it is the default: if you don't specify any Constructor, it is created automatically.

如果您改为指定另一个构造函数,例如具有类似您的参数的构造函数,则不会自动创建no-args构造函数,并且您 必须 明确声明它如果需要的话.

If instead you specify another Constructor, for example one with parameters like your, the no-args Constructor is not automatically created anymore, and you have to declare it explicitly if you need it.

Struts2需要no-args构造函数来创建您的bean.

Struts2 needs the no-args Constructor to create your beans.

例如,您可能具有一个带有10个参数的Constructor的bean,并在JSP页面中仅指定其中一个:Struts必须能够创建对象并设置单个字段(通过Setter),而不必关心缺少的九个参数.

For example, you could have a bean with a Constructor taking 10 parameters, and specify only one of them in the JSP page: Struts must be able to create the object and set the single field (through the Setter) without caring about the nine missing parameters.

这篇关于从jsp到action类获取List对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆