从jsp到action类获取List对象值

2022-01-16 00:00:00 java jsp struts2

在 JSP 中迭代 List 对象,其值来自正确显示的 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;
    }   
}

豆子:

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.

推荐答案

添加一个 default no-args Constructor 到你的 Bean 类.

Add a default no-args Constructor to your Bean Class.

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.

例如,您可以有一个带有 Constructor 的 bean,它带有 10 个参数,并在 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.

相关文章