Struts 1.2 - 显示和更新ArrayList项

时间:2014-04-01 06:19:37

标签: jsp struts-1

我想迭代ArrayList元素并更新列表元素然后保存它。

My Form Bean:
MyTestCodeForm.java - it have two members id and ArrayList.
 String id;
 ArrayList<Book> listBook; 

id属性的getter和setter  ArrayList的getter和setter

 public void setListBook(ArrayList<Book> bookList)
 {
  this.listBook = bookList;
 }

 public ArrayList<Book> getListBook()
 {
   return this.listBook;
 }

Book.java have two members
 String bookId;
 String bookName;


Action class - MyTestCodeAction.java

在此,我已获取数据并保存到数据库中。

我的jsp页面用于迭代:

<nested:nest property='myTestCodeForm'>
<html:form action='/myTestCodeForm'>

<nested:write name='' property='id'/>
<html:hidden name='' property='id'/>
<nested:iterate id='foo' name='' property='bookList'>

  <html:text name='foo' property='bookName' indexd='true'/>

</nested:iterate>
<html:submit value='submit'/>

</html:form>

</nested:nest>

我的问题是,我成功迭代数据但是当我将数据导入动作类时,我没有收到数据列表数据,但我收到了id属性。

请帮忙

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,我在FormBean类中错过了一些东西。在我使用FormBean Class之前,一个用于获取ArrayList元素的getter和一个用于设置ArrayList元素的setter但是我没有为Book类创建getter和setter,在为Book类创建getter和setter之后我获得了Action类中的值。现在,My Form Bean看起来像这样:

 public void setListBook(ArrayList<Book> bookList)
 {
  this.listBook = bookList;
 }

 public ArrayList<Book> getListBook()
 {
   return this.listBook;
 }
 public Book getBook(int index)
{
  if(index >= index)
  {
    this.listBook.add(new Book());
  }

  return (Book)this.listBook.get(index);
}
 public Book setBook(int index, Book book)
{
  return this.listBook.set(index, book);
}