File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
Don't get me started about those stupid light bulbs.
 
subject: ArrayLists and Submit
 
Similar Threads
html:select and html:options or html:optionsCollection
Date difference, with the table containing null values for date
Validation of indexed properties
problem in retriving value
How to crate Multiple ArrayList from one ArrayList