This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ArrayLists and Submit" Watch "ArrayLists and Submit" New topic
Author

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.


Merrill
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: http://aspose.com/file-tools
 
subject: ArrayLists and Submit