获取从jsp到action类的List对象值

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

以下是jps代码。

                

ViewAction.java和Bean类代码如下

在动作类列表中,对象名是beanList

 public class ViewCheckboxAction extends ActionSupport { HttpServletRequest request = ServletActionContext.getRequest(); String viewData = "select * from student order by rollno"; List beanList; public List getBeanList() { return beanList; } public void setBeanList(ArrayList beanList) { this.beanList = beanList; } public String execute() { beanList = new ArrayList(); 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类的多/单更新表单字段值 ,以便执行更新的操作。 但是list(beanList)值在action类中无效。 由于它无效,我无法进行更新操作。 1)在新的动作类(EditAction.java)中如何初始化列表对象( beanList )? 它与我在ViewAction.java中声明的方式相同2)Jsp sysntax是否合适? 请求您提供帮助。 提前致谢。

Bean类添加默认的no-args构造函数

默认的no-args构造函数被调用,因为它是默认值:如果没有指定任何构造函数,它将自动创建。

如果您指定另一个构造函数,例如一个带有像您这样的参数的构造函数,则不再自动创建no-args构造函数,如果需要,您必须明确声明它。

Struts2需要no-args构造函数来创建bean。

例如,你可以让一个带有构造函数的bean获取10个参数,并在JSP页面中只指定其中一个:Struts必须能够创建对象并设置单个字段(通过Setter)而不关心九个缺失参数。

您必须使用类型转换,在ViewCheckboxAction-conversion.properties文件中提供以下配置:

 KeyProperty_beanList=rollnumber Element_beanList=Bean CreateIfNull_beanList=true 

通过表单提交时,rollnumber用作beanList中Bean实例的KeyProperty。您可以使用Key Property字段的任何其他属性。 name的值将设置为具有此特殊ID的MyBean实例。 列表没有为不可用的id值添加空值。 这种方法避免了OutOfMemoryErrors的风险!

                 

请参阅: http : //struts.apache.org/release/2.0.x/docs/type-conversion.html