Using a collection of Beans within an ActionForm

2003-11-19 Thread Webb, Ed
I need to display several lists of choice to the user. These lists are
variable in length depending on the user. I use an ActionForm which contain
Lists. I populate these with objects that contain String and boolean
properties (name of option and selected flag). I add this to the request
scope under the mapping.getAttribute() key and everything is displayed fine
in my JSP page. However when it comes time to submit the changes a new
ActionForm is created with empty Lists and unsurprisingly I get the error
message:

exception:
javax.servlet.ServletException: BeanUtils.populate 
...
root cause:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 
...

Here's a snippet from the JSP page to show how I'm displaying the
information:
...
c:forEach varStatus=status var=media items=${emailalertForm.media}
tr
tdfmt:message key=${media.mediaName} //td
tdhtml:checkbox
property=media[${status.index}].selected //td  /tr
/c:forEach
...
The question is how can I display lists of choices to the user and get the
changes the user makes to the checkboxes back to my Action? I've read about
nested beans but the only examples I found only had a single bean and not a
collection. Is it possible or is there a better way to go about this?

Thanks for any advice or pointers,

Ed!


Re: Using a collection of Beans within an ActionForm

2003-11-19 Thread Nicholas L Mohler





Ed,

You can find examples of this by searching the archive for indexed
properties.  You are getting this exception because the Form collection
does not have enough entries to support the property that you are trying to
populate.  You need to insure that the getter that returns a single bean in
the collection (for a given index) creates bean entries in the collection
up to the index that you are trying to retrieve.

There are a few ways to do this.  My getter method:

public CollectionBean getCollectionName(int index) {
 while(index = collectionName.size()) {
  collectionName.add(new CollectionBean())
 }
 return (CollectionBean)collectionName.get(index);
}




   

  Webb, Ed   

  [EMAIL PROTECTED]To:   [EMAIL PROTECTED]

  mation.com  cc: 

   Subject:  Using a collection of 
Beans within an ActionForm  
  11/19/2003 07:11 AM  

  Please respond to Struts

  Users Mailing List  

   

   





I need to display several lists of choice to the user. These lists are
variable in length depending on the user. I use an ActionForm which contain
Lists. I populate these with objects that contain String and boolean
properties (name of option and selected flag). I add this to the request
scope under the mapping.getAttribute() key and everything is displayed fine
in my JSP page. However when it comes time to submit the changes a new
ActionForm is created with empty Lists and unsurprisingly I get the error
message:

exception:
javax.servlet.ServletException: BeanUtils.populate
...
root cause:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
...

Here's a snippet from the JSP page to show how I'm displaying the
information:
...
c:forEach varStatus=status var=media items=${emailalertForm.media}
 tr
 tdfmt:message key=${media.mediaName} //td
 tdhtml:checkbox
property=media[${status.index}].selected //td   /tr
/c:forEach
...
The question is how can I display lists of choices to the user and get the
changes the user makes to the checkboxes back to my Action? I've read about
nested beans but the only examples I found only had a single bean and not a
collection. Is it possible or is there a better way to go about this?

Thanks for any advice or pointers,

Ed!





-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Using a collection of Beans within an ActionForm [Solved]

2003-11-19 Thread Webb, Ed
Thanks very much, that was just the nudge in the right direction. I found
the http://jakarta.apache.org/struts/faqs/indexedprops.html page very
helpful. As I am using EL and the JSTL tags I found that creating a method:

public List getMedia(int index) { ... }

as that page suggested (List-backed Indexed Properties) caused the
c:forEach tag to complain it couldn't find a getter for property media so
I had to create 2 methods:

public List getMedia() { 
return media;
} 

public MediaBean getMedium(int index) {
 while(index = media.size()) {
  media.add(new MediaBean())
 }
 return (MediaBean)media.get(index);
}

and use this in the JSP:

c:forEach varStatus=status var=med items=${emailalertForm.media}
tr
tdfmt:message key=${med.mediaName} //td
tdhtml:checkbox
property=medium[${status.index}].selected //td /tr
/c:forEach

to get everything to display and return correctly.

Cheers,

Ed!

-Original Message-
From: Nicholas L Mohler [mailto:[EMAIL PROTECTED]
Sent: 19 November 2003 12:45
To: Struts Users Mailing List
Subject: Re: Using a collection of Beans within an ActionForm

Ed,

You can find examples of this by searching the archive for indexed
properties.  

[snip]

There are a few ways to do this.  My getter method:

public CollectionBean getCollectionName(int index) {
 while(index = collectionName.size()) {
  collectionName.add(new CollectionBean())
 }
 return (CollectionBean)collectionName.get(index);
}