• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayLists and Submit

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic