You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Miguel Arroz <ar...@guiamac.com> on 2004/05/29 15:59:40 UTC

Multiple submit buttons in the same form

Hi!

   Can I have multiple submit buttons on the same form? Imagine I have 
"Button A" and "Button B" on a JSP, and I want to do slightly different 
things when a user clicks on each button. It's enough to detect, on the 
Action, the button the user clicked... but that's precisely my 
question. How can I know what button did the user pressed to submit the 
form?

   Yours

Miguel Arroz

                                                  Fire, walk with me.

   Miguel Arroz - arroz@guiamac.com - http://www.guiamac.com


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Multiple submit buttons in the same form

Posted by Riyad Kalla <rs...@email.arizona.edu>.
Rick,
This is a great approach as well and I think we've giving good answers. 
1 way you change the Form, and another way you change the Action.

I didn't know you could do it the way you described, thanks for the tip!

Rick Reumann wrote:

> Riyad Kalla wrote:
>
>> 1) Yes you can do it
>> 2) Given each button a different value, like "List Products" and 
>> "List All Products"
>> 3) Add a "buttonValue" (or some other adequetly names) property to 
>> your form for the respective action, it will capture the button clicked.
>> 4) In your action:
>>
>> if(form.getButtonValue().equals("List Products"))
>>    // do stuff
>> else if(form.getButtonValue().equals("List All Products"))
>>    // do more stuff
>
>
> You can do it much easier using either of the approachs below:
>
> 1) Use the LookUpDispatch Action. You simply give the submit button 
> your dispatch property name (ie property="UserAction" ) then the value 
> of the button gets looked up from a map in your LookUpDispatchAction 
> (see the docs). Much cleaner.. although..
>
> 2) I still prefer to set a hidden variable of a dispatch Action with 
> javascript when a button is clicked. Then simply use a regular 
> DispatchAction or someone mentioned even a MapplingDispatchAction 
> which I wasn't aware of (maybe this later is in Struts 1.2?). Assuming 
> a regular dispatch action you have buttons like..
>
> <html:submit 
> onclick="document.yourForm.userAction.value='Update'">Update</html:submit> 
>
> <html:submit 
> onclick="document.yourForm.userAction.value='Cancel'">Cancel</html:submit> 
>
>
> <html:hidden property="userAction"/>
>
> Then just make sure you are using a DispatchAction. (In the above it's 
> better if you use the resources properties file for you button names: 
> <bean:message key="button.update"/> Just in case later the button 
> names change, it's easier to modify one resources file.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Multiple submit buttons in the same form

Posted by Rick Reumann <st...@reumann.net>.
Riyad Kalla wrote:

> 1) Yes you can do it
> 2) Given each button a different value, like "List Products" and "List 
> All Products"
> 3) Add a "buttonValue" (or some other adequetly names) property to your 
> form for the respective action, it will capture the button clicked.
> 4) In your action:
> 
> if(form.getButtonValue().equals("List Products"))
>    // do stuff
> else if(form.getButtonValue().equals("List All Products"))
>    // do more stuff

You can do it much easier using either of the approachs below:

1) Use the LookUpDispatch Action. You simply give the submit button your 
dispatch property name (ie property="UserAction" ) then the value of the 
button gets looked up from a map in your LookUpDispatchAction (see the 
docs). Much cleaner.. although..

2) I still prefer to set a hidden variable of a dispatch Action with 
javascript when a button is clicked. Then simply use a regular 
DispatchAction or someone mentioned even a MapplingDispatchAction which 
I wasn't aware of (maybe this later is in Struts 1.2?). Assuming a 
regular dispatch action you have buttons like..

<html:submit 
onclick="document.yourForm.userAction.value='Update'">Update</html:submit>
<html:submit 
onclick="document.yourForm.userAction.value='Cancel'">Cancel</html:submit>

<html:hidden property="userAction"/>

Then just make sure you are using a DispatchAction. (In the above it's 
better if you use the resources properties file for you button names: 
<bean:message key="button.update"/> Just in case later the button names 
change, it's easier to modify one resources file.

-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Multiple submit buttons in the same form

Posted by Riyad Kalla <rs...@email.arizona.edu>.
1) Yes you can do it
2) Given each button a different value, like "List Products" and "List 
All Products"
3) Add a "buttonValue" (or some other adequetly names) property to your 
form for the respective action, it will capture the button clicked.
4) In your action:

if(form.getButtonValue().equals("List Products"))
    // do stuff
else if(form.getButtonValue().equals("List All Products"))
    // do more stuff

Best,
Riyad

Miguel Arroz wrote:

> Hi!
>
>   Can I have multiple submit buttons on the same form? Imagine I have 
> "Button A" and "Button B" on a JSP, and I want to do slightly 
> different things when a user clicks on each button. It's enough to 
> detect, on the Action, the button the user clicked... but that's 
> precisely my question. How can I know what button did the user pressed 
> to submit the form?
>
>   Yours
>
> Miguel Arroz
>
>                                                  Fire, walk with me.
>
>   Miguel Arroz - arroz@guiamac.com - http://www.guiamac.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org