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

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

问题描述

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

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

下面是 jps 代码.

Below is the jps code.

<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;
    }   
}

豆子:

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 到 action 的多个/单个更新的表单字段值class 以进行更新操作.但是列表(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 类添加默认无参数构造函数.

default no-args Constructor 被这样调用是因为它是默认的:如果你不指定 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.

如果您改为指定另一个构造函数,例如一个带有像您这样的参数的构造函数,则不会再自动创建无参数构造函数,并且您必须明确声明它如果你需要它.

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 需要无参数构造函数来创建您的 bean.

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

例如,你可以有一个带有 10 个参数的构造函数的 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获取List对象值到action类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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