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 Struts: How does indexed data get into a Form Bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: How does indexed data get into a Form Bean?" Watch "Struts: How does indexed data get into a Form Bean?" New topic
Author

Struts: How does indexed data get into a Form Bean?

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Hi folks,

I'm working on my first Struts app and I've stumbled across something I just don't know how to handle.

I have a form that uses nested:iterate to display a large amount of data to the user. I'm actually showing them error records in a database so that they can correct them.

Anyway, I can get the information to display on the form using a construct something like this:



And so on and so forth. I used the "indexed" attribute because, when they submit this form, I need to be able to differentiate between the "First Name" of the first record and the "First Name" of the second record and right down the line. Using that attribute works great in that my HTML ends up looking like this:



That allows me to tell one from the next, but I've got a new problem now. When this form is submitted, how is this information inserted into the form bean? Normally (not using indexed fields), the text field named "firstName" would get inserted into the "firstName" field via the setFirstName(String) method. However, now, I don't have one first name, I have a bunch and I don't really know how many there will be ahead of time.

I tried to write a simple form bean that looks like this:



Unfortunately, nothing at all is being inserted into my ArrayList. Have I named my field or my methods incorrectly? I must be missing something here.

Thanks,
Corey


SCJP Tipline, etc.
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
You need to create a getter method which accepts the index as an int.

OR

Check this link for understanding how indexed properties work.
Indexed Properties, Mapped Properties, and Indexed

Sheldon Fernandes
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
After having a second look at your post, what I suggested above will not work. It appears that your form bean has a collection of FileUploadExceptionsBean, and each FileUploadExceptionsBean has a collection of first names. In this case your generated html should look something like this.

<input type="text" name="exceptionBean[0].firstName[0]" value="Corey">
<input type="text" name="exceptionBean[0].firstName[1]" value="Corey1">
<input type="text" name="exceptionBean[0].firstName[2]" value="Corey2">
<input type="text" name="exceptionBean[1].firstName[0]" value="Bob">
<input type="text" name="exceptionBean[1].firstName[1]" value="Bob1">
<input type="text" name="exceptionBean[1].firstName[2]" value="Bob2">

This is what the framework will do (when you submit the html form),
- search for method getExceptionBean(int) in your form
- search for method setFirstName(int, String) in the object returned from first search

So you will need a setter method too. Anyway the link above is a must read. Read the part about The Wrinkle with Indexed Tags.

I find this stuff tricky at times, hope you are able to figure it out.
Sheldon Fernandes
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Unfortunately, I'm still having problems with this. My form bean contains 3 collections, one for each type of exception I'm dealing with. Each of those collections contains a list of beans, which, individually, represent a single exception.

Here's a look at my form bean:



For now, let's just concentrate on one Collection of the three - let's say we're going to look at the Blood Pressure Exceptions. The Collection bloodPressureException contains a number of BloodPressureException objects, that look like this (they're not Exceptions in the normal sense as they don't get thrown - they're exceptions to business rules):



As you can see, BloodPressureException is just a simple Java Bean. Now, in order to display this information to the user, I use a JSP that looks something like this:



Using that, I get a nice display of all the information in tables displayed top to bottom. The actual HTML that is generated looks like this:



Okay - so far, everything works great. I can display all of my information to the user and it all appears in nice, editable fields so that the user can fix errors and resubmit the data for processing.

My problem really arises when I need Struts to take the information that is submitted into the request and insert it into my form bean. I don't know what the signature of the method(s) it is calling and I can't seem to get it right, either.

My inclination, since each field is named exceptionBean[n].propertyName was that my form bean would need a method with this signature:



Once it has the BloodPressureException object, it would simply invoke the setXXX() method, based on the property that it was trying to set.

Unfortunately, this doesn't seem to work. I've added that method to my form bean (and a few variations of it), but none of them are invoked. Anyone have any ideas?

Thanks,
Corey
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I don't know what happened - if it was me rewriting some of the code for this post or what, but I am now getting Struts to invoke the getExceptionBean(int index) method so at least I know it's trying to set the properties.

Unfortunately, I seem to have gone from one problem right to another.

Now, it successfully sets the first property (whatever that might be), but throws an IllegalArgumentException when trying to set the second property. At this point, I'm puzzling over how to debug this. It doesn't seem to provide me with any information as to which property it is trying to set and is causing the error.

Anyone know how I might get around to debugging this?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I seem to have found the problem. Apparently, Struts can not set a java.util.Date object in my bean - I need to set it as a String and then I can convert it later.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

See this article for converting Date


Junilu - [How to Ask Questions] [How to Answer Questions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts: How does indexed data get into a Form Bean?