• 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

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

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this article for converting Date
 
reply
    Bookmark Topic Watch Topic
  • New Topic