• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to obtain the id of a clicked button

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

i have a problem concerning a dispatch action. I pass the method name with the submit. However i also need an extra parameter. I am not sure how to pass on this value.

So i stopped using the html:submit tag and instead used :


As you can see i experimented by setting the product id as the id of the button.

Now i would like to know how i can obtain the id of the clicked button in my action.

I know there are several javascript possibilities to set a certain value on click of the button. I would prefer not using javascript so can someone please help me out?!!
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "id" attribute of an html tag is for javaScript use only. It does not get passed to the server when the page is submitted. If you want to pass the product ID to the server when the page is submitted, use a hidden field (<html:hidden> or <input type="hidden">).

[ February 22, 2006: Message edited by: Merrill Higginson ]
[ February 22, 2006: Message edited by: Merrill Higginson ]
 
Ricardo Estafan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks.

However i've tried to do so, but somehow the wrong value gets passed :



It somehow passes the id of the previous product...
 
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
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:

<logic:iterate>
<html:form ...>
<input type="hidden" ...>
<html:submit ... />
</html:form>
</logic:iterate>

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.
 
Ricardo Estafan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Could anyone please help me out on this issue?
 
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
Ricardo,

You now have a couple of options:

1- Keep the multiple forms design of the page, and for each product, have both a delete button and an "update this product" button for each product. This way when the update button is pressed, the action will have access to both the product ID and the amount. This is the only way I see it for you to avoid using JavaScript.

2- Bite the bullet and use some JavaScript. In my experience, it is nearly impossible to create a robust, friendly user interface without the use of at least some JavaScript. Here's a possible solution:

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 ]
 
expectation is the root of all heartache - shakespeare. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic