• 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

ActionForm with indexed properties

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having difficulties with ActionForm when dealing with indexed properties.
I have a form that has varying number of product fields - it is a shopping chart that consist of products with four parameters: id, name, price and quantity.

For that I have created JavaBean "Item":


So the form consist of indexed properties like that:


So the data is sent like so:
item[0].id=""; item[0].name=""; item[0].price=""; item[0].quantity="";
item[1].id=""; item[1].name=""; item[1].price=""; item[1].quantity="";
....

Now what confuses me, is how to create a ActionForm. After reading punch of other posts and articles about it, this is what I have comed up with:


It feels that this is not it - it seems too simple And allso I have no idea how to make a validate method, for chacking the quantity parameter, that is set in input, type=text.

Anyway I think there is no reason, but to be sure, I allso post the part of Action class where I try to get access to the ArrayList of Items:


So after I submit the form I get exception HTTP Status 500 - :


And this is where I am out of ideas. Please help.
[ May 16, 2006: Message edited by: Juhan Voolaid ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Try changing the scope of the Form to Session..

Thanks
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the text input fields for quantity parameters, so I believe I can pass them only by request scope, with POST method.
[ May 17, 2006: Message edited by: Juhan Voolaid ]
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
up
 
Soumya Saha
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Juhan,
Did it work? ..If so how?

Thanks
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I still havent figred it out and I think your suggestion does not solve my problem.
 
Soumya Saha
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.

Thanks
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone else?
 
Ranch Hand
Posts: 4864
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, Soumya's suggestion about putting your Action form in session scope was a good one. Here's why:

If the ActionForm is in request scope, once the page is displayed the ActionForm bean is gone. That's the definition of request scope: it only lasts the duration of one request. When the form is submitted, Struts instantiates a new Action form, but there's a problem: The ArrayList of products that you populated before the page was diplayed is no longer there and is either null or empty. So, when struts tries to populate item[0}, there is no item[0]. The easiest way to fix this is to put the ActionForm in session scope.

However, if you really don't want to put it in session scope, you can use a "lazy initialization" pattern to rebuild your List of Items. This link explains more about the technique.

Another thing that could be causing this problem is the properties in your Item bean. Remember that web pages deal in Strings. If you defined your setPrice or setQuantity methods to accept types such as Integer or BigDecimal, that's a problem and will cause the error you've displayed. You must have a setter that accepts a String in order for Struts to populate the property for you. If you want to turn it into another type, that's fine, but it has to accept a String.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx for the reply. I have made a small progress - I've got rid of the Exceptions, by fallowing the link and doing so with my ActionForm class, but I get no properties back in my Action class.

This is how my new ActionForm looks like:

I think because now I have the method "populateItems", it gets rid of the Exception I got before, but I don't really now what is the point of that method.

And if you were conserned if my Item bean has all properties Strings, then yes, I have checked and they are all Strings.

But now to the problem. I think there is still something wrong with my ActionForm, because I don't get the input back, I inserted to the form.

This is how I try to access the ArrayList of items in my Action class:

The size of the items ArrayList is 0, but in my (html)form there were several input fields that I submitted. I have no idea what is wrong.

This is the form in HTML:


So if you are noticing what I am missing here, just let me know.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I got it working - thanx everybody.

I took a second look to that link and I noticed that I didn't have a form property "items" defined in my struts-conf.xml. Also the input properties were item[x].name, but it should have been items[x].name.

So if anyone is still stuck with similar example. I post the code below. The ActionForm remained the same.

This is what I added to struts-conf.xml:


And the HTML form consist of similar tags:

[ May 23, 2006: Message edited by: Juhan Voolaid ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changing item[0] to items[0] is likely what solved the problem.

A <form-property> tag in your struts-config.xml is only used if your ActionForm is DynaActionForm or a subclass of DynaActionForm. From the code you showed us, your form extends ActionForm, not DynaActionForm. It obviously doesn't hurt anything, but it isn't necessary and isn't used. I just wanted to make this clear for any others experiencing similar issues.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh thnx. Yes I don't use DynaActionForm. I like to validate my form manually.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi can you place the entire code
with JSP, Action Form, Actiopn Class and entries in struts-config.xml

I am still struggling. probably your code can give me better idea.

Thanks in advance.

Rajesh Renke
reply
    Bookmark Topic Watch Topic
  • New Topic