You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alex Lindgren <li...@ureach.com> on 2002/08/06 01:05:38 UTC

updating actionforms that contain collections (and are displayed using the nested taglib)

Hello Struts users,

I'm fairly new to struts but I think I've done enough research (search the web, mailing list archives, etc) and tinkering to ask this question. In fact, I've already figured out a solution to my problem -- but I want to know if it is a good solution. Also, judging from the list archives it seems to be a topic of considerable confusion so I hope to perhaps eventually contribute a tutorial or some other form documentation so others don't have to dig so deep into mail archives.

Scenario: I have an ActionForm that has nested properties. In other words, it has a property that is a collection of beans (another ActionForm). I am using the nested tags to create an html form. I use the iterate tag to iterate through the collecton and display input elements for several properties for each item in the collection. I should also mention that I am trying to avoid putting things in sessions unless they really need to be there.

Problem: The page displays alright (the nested tags work great - thanks keyboard monkey!), but when the form is submitted, it has a tough time knowing what to do with the nested inputs. In fact, I was getting a null pointer or ArrayIndexOutOfBoundsException.

Diagnosis: The heart of the problem seems to be that since I am not using the session to keep the object around, the collections weren't being recreated when struts created the ActionForm for the following request. I got null pointers/ArrayIndexOutOfBoundsException because it was trying to access an object that wasn't there. (Note that I created the ActionForm for the initial request in the Action and then put in the request object and shipped off to the JSP to be displayed.)

Solutions:

1. Put the bean in session scope. (I didn't try this because I really want to avoid using sessions). The examples at http://www.keyboardmonkey.com/next/ use sessions.

2. Use the ActionForm reset() method to load the collection. Since the reset method has access to the request object, it can get the id and query the database. One caveat: I am using the LazyMap for my collection after reading this suggestion by the nested taglib author: "Wrap your collections in org.apache.commons.collections.LazyList, provide a class definition of your child bean and it'll be sweet and ready to do without any other effort, even in the reset() method"
(http://www.mail-archive.com/struts-user@jakarta.apache.org/msg36486.html). I am not sure if it is necessary to use a lazy collection but I suspect it is since the struts framework seems to use reflection and needs to know about the class stored in the collection. Since I didn't find docs on this class here is how the collection is created:
java.util.Map lazyMap  = LazyCollections.lazyMap( map, MyActionForm.class );

Finally, my question is, is this use of the reset() method appropriate and necessary?  Will it create some unforeseen problems? Will recreating the collection each time cause a problem with the order in which case it might confuse which item in a collection is which? 

I'd appreciate any feedback. Thanks.

-Alex