File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes ArrayLists and Submit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ArrayLists and Submit" Watch "ArrayLists and Submit" New topic

ArrayLists and Submit

Thara Visu
Ranch Hand

Joined: May 17, 2005
Posts: 87
Hi all,
I have a ArrayList of PersonBeans (that contains name) in my formbean.

I iterate through the arrayList and the text boxes are displayed.

On click of submit, in my action if i retrieve my arraylist from the formbean, the arraylist size is always zero. Why is it?

My form bean is request scoped.

Thanks in advance.

Thara<br />SCJP 1.4 96%<br />SCBCD 1.3 96%
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In a request scoped bean, everything in it goes away after the page is displayed, including the array list. For a regular text field, this is OK, because when the user submits the form, Struts creates a new instance of the form bean and populates the property with what the user entered on the form. However, an ArrayList that you put in the form bean before the page was displayed does not get rebuilt automatically. The newly instantiated ActionForm bean has, at best, an empty one.

You have 2 choices:

1-Change the scope of the ActionForm to session.
2-Override the reset(...) method of ActionForm and in it add logic to rebuild your array list from the database or whatever source it comes from.

Make sure that you have used indexed properties on your JSP, and that you have indexed getters and setters in your form bean.

See this link for more information on indexed properties.

Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
This is how I code the get method for my indexed properties. You don't need the null check if you initialize your list in the constructor or where the variable is declared. You will find that the indexes get submitted in what seem like random order (10, 3, 5, ...). That is why the second block of code is a loop.

- Brent
I agree. Here's the link:
subject: ArrayLists and Submit
It's not a secret anymore!