[ February 22, 2006: Message edited by: Merrill Higginson ] [ February 22, 2006: Message edited by: Merrill Higginson ]
If you're iterating through a collection, you're creating multiple hidden fields all with the same name and different values. When the form is submitted, Struts is going to place the first one in the Form bean.
One possible solution would be to make each product line a separate form following this pattern:
This way, only the product ID enclosed in a single <html:form> tag set will be submitted when the submit button is pressed.
If you do this, you will undoubtedly have to chang other parts of the page, though. Since this appears to be a shopping cart, I asuume you have a "checkout" button. This button would now have to be in it's own <html:form> tag set.
Ok this worked fine. However a new issue arises ... <<sigh>>
Within the first form tag (which contains the delete submit) I declare the following :
At the bottom of the page i indeed have a checkout in my case order button. However i also have an update button. The action uses the above declared Id and amount values to update the amounts contained by the shoppingcart.
When i try to obtain the values of Id and amount in my action both are null. Probably because they are not contained within the same form tag as the submit.
Redesign your ActionForm so that one of the properties is of type java.util.List, and contains a list of products. You should have a Product class with has an ID and an amount property.
Change your JSP back to using a single <html:form> tag pair for the entire document and use indexed properties for the product ID and amount. Read this link to understand how to use indexed properties. Your property attribute will look something like: property='<%="product["+index+"].id" %>'
Add a single hidden field to the form to contain the product ID of the product to be deleted if one of the delete buttons is pressed. Change your delete buttons from submit buttons to a regular buttons (<html:button>) and have the onclick event of those buttons place the current product Id (the product ID just preceding the delete button) into the deleted product ID field. Then have it submit the form. This way your Action class can tell which product should be deleted. [ February 24, 2006: Message edited by: Merrill Higginson ]