This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.id=""; item.name=""; item.price=""; item.quantity=""; item.id=""; item.name=""; item.price=""; item.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 ]
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. 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.
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.
Joined: Nov 18, 2003
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 ]
Joined: Feb 15, 2005
Changing item to items 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.
Joined: Nov 18, 2003
Oh thnx. Yes I don't use DynaActionForm. I like to validate my form manually.