You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jason Dolinger <jd...@lab49.com> on 2008/01/29 04:53:34 UTC

best way to handle multiple buttons in a form?

Hi all,

I'm new to Struts 2 (and Struts in general), and I'm having a hard time figuring out how to handle the simple case of multiple buttons within a form.  The method I'm trying to use is described in the Struts 2 cookbook here:  http://struts.apache.org/2.x/docs/html-form-buttons-howto.html.

I've copied these verbatim (along with a small struts.xml to wire them up), and both button boolean fields always remain false.  Should this work?  I'm using Struts 2.0.11.  There's always the route of using Javascript to detect which button was clicked, but I'd rather avoid it if not necessary.  

I've also seen mention of a Struts LookupDispatcher or MapDispatcher.  Is there something similar in Struts 2?  

Thanks, any input is greatly appreciated!

Regards,
Jason




Re: best way to handle multiple buttons in a form?

Posted by Laurie Harper <la...@holoweb.net>.
Jason Dolinger wrote:
> Hi all,
> 
> I'm new to Struts 2 (and Struts in general), and I'm having a hard time figuring out how to handle the simple case of multiple buttons within a form.  The method I'm trying to use is described in the Struts 2 cookbook here:  http://struts.apache.org/2.x/docs/html-form-buttons-howto.html.
> 
> I've copied these verbatim (along with a small struts.xml to wire them up), and both button boolean fields always remain false.  Should this work?  I'm using Struts 2.0.11.  There's always the route of using Javascript to detect which button was clicked, but I'd rather avoid it if not necessary.  


Are you sure you copied the example code faithfully? It should work as 
advertised. Post your action mapping configuration and the action 
code/JSP markup you're actually using; maybe there's a simple mistake in 
there someone can spot.

> I've also seen mention of a Struts LookupDispatcher or MapDispatcher.  Is there something similar in Struts 2?  

There are no direct equivalents of those classes in Struts2 because they 
  aren't needed. The Struts2 core includes mechanisms that allow you to 
achieve the same things more directly. See, for example, the 'method' 
attribute on action mappings and on the s:form and s:submit tags.

L.


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


Re: best way to handle multiple buttons in a form?

Posted by st...@gmail.com.
Why not use the method attribute of the tag?  This allows you to "wire" a
button to a method on the action.

On Feb 13, 2008 6:47 PM, akinss <ak...@gmail.com> wrote:

>
> Yep I've had the same problem and it has been registered in the Struts 2
> jira.
>
> https://issues.apache.org/struts/browse/WW-2362
>
> Not sure how to work around it though.
>
> Steve
>
>
> Jason Dolinger wrote:
> >
> > Hi all,
> >
> > I'm new to Struts 2 (and Struts in general), and I'm having a hard time
> > figuring out how to handle the simple case of multiple buttons within a
> > form.  The method I'm trying to use is described in the Struts 2
> cookbook
> > here:  http://struts.apache.org/2.x/docs/html-form-buttons-howto.html.
> >
> > I've copied these verbatim (along with a small struts.xml to wire them
> > up), and both button boolean fields always remain false.  Should this
> > work?  I'm using Struts 2.0.11.  There's always the route of using
> > Javascript to detect which button was clicked, but I'd rather avoid it
> if
> > not necessary.
> >
> > I've also seen mention of a Struts LookupDispatcher or MapDispatcher.
>  Is
> > there something similar in Struts 2?
> >
> > Thanks, any input is greatly appreciated!
> >
> > Regards,
> > Jason
> >
> >
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/best-way-to-handle-multiple-buttons-in-a-form--tp15151826p15471656.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Scott
stanlick@gmail.com

Re: best way to handle multiple buttons in a form?

Posted by akinss <ak...@gmail.com>.
Spot on.

There are probably several other ways to do routing based on Button selects
too.  

Using:
 <s:submit name="redirect-action:doThis" value="This"/>

in the tag is another way of handling Buttons.  You could then inject a
boolean value as an Action mapping parameter.
eg.
<action name="doThis" class="somethingClass" method="doSomethingMethod">
          true

Thinking more on it, using booleans to define control/flow is probably a bad
thing.

So,  either as you said, use 
<s:form action="/doSomething">
  <s:submit name="anything"   method="doThisMethod" value="This"/>
</s:form>

<action name="doSomething" class="somethingClass" method="defaultMethod">

Or,
<s:form action="/doDefault">
  <s:submit name="redirect-action:doThis" value="This"/>
</s:form>

<action name="doThis" class="somethingClass" method="doThisMethod">
...
<action name="doDefault" class="somethingClass" method="doDifferentMethod">
...

I guess which way you choose comes down to what you need in the Action
Mapping.
If you don't have different interceptor stacks or parameter injection, etc.
using the method= method is probably the best way.  

The redirect-action way means you need to define separate Action Mapping
definitions for each of the buttons as well as the default action.  This
might be a good thing if they need to be configured differently.

Both of these methods give you a default process if no button is selected.

I guess maybe all that's wrong here is the HOWTO page.   Should this be
updated?
Using the methods described, you know which button is pressed by which
method is executed.  This is far better than testing booleans.

The "Dynamic Set Of Buttons" section on that page works and is useful when
dealing with lists of data.    

Hope this helps and makes some sort of sense.
Steve


-- 
View this message in context: http://www.nabble.com/best-way-to-handle-multiple-buttons-in-a-form--tp15151826p15473920.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: best way to handle multiple buttons in a form?

Posted by "Alberto A. Flores" <aa...@gmail.com>.
Have you tried using the "method" attribute (of the tag)?

akinss wrote:
> Yep I've had the same problem and it has been registered in the Struts 2
> jira.
> 
> https://issues.apache.org/struts/browse/WW-2362
> 
> Not sure how to work around it though.
> 
> Steve
> 
> 
> Jason Dolinger wrote:
>> Hi all,
>>
>> I'm new to Struts 2 (and Struts in general), and I'm having a hard time
>> figuring out how to handle the simple case of multiple buttons within a
>> form.  The method I'm trying to use is described in the Struts 2 cookbook
>> here:  http://struts.apache.org/2.x/docs/html-form-buttons-howto.html.
>>
>> I've copied these verbatim (along with a small struts.xml to wire them
>> up), and both button boolean fields always remain false.  Should this
>> work?  I'm using Struts 2.0.11.  There's always the route of using
>> Javascript to detect which button was clicked, but I'd rather avoid it if
>> not necessary.  
>>
>> I've also seen mention of a Struts LookupDispatcher or MapDispatcher.  Is
>> there something similar in Struts 2?  
>>
>> Thanks, any input is greatly appreciated!
>>
>> Regards,
>> Jason
>>
>>
>>
>>
>>
> 

-- 

Alberto A. Flores
http://www.linkedin.com/in/aflores



Re: best way to handle multiple buttons in a form?

Posted by akinss <ak...@gmail.com>.
Yep I've had the same problem and it has been registered in the Struts 2
jira.

https://issues.apache.org/struts/browse/WW-2362

Not sure how to work around it though.

Steve


Jason Dolinger wrote:
> 
> Hi all,
> 
> I'm new to Struts 2 (and Struts in general), and I'm having a hard time
> figuring out how to handle the simple case of multiple buttons within a
> form.  The method I'm trying to use is described in the Struts 2 cookbook
> here:  http://struts.apache.org/2.x/docs/html-form-buttons-howto.html.
> 
> I've copied these verbatim (along with a small struts.xml to wire them
> up), and both button boolean fields always remain false.  Should this
> work?  I'm using Struts 2.0.11.  There's always the route of using
> Javascript to detect which button was clicked, but I'd rather avoid it if
> not necessary.  
> 
> I've also seen mention of a Struts LookupDispatcher or MapDispatcher.  Is
> there something similar in Struts 2?  
> 
> Thanks, any input is greatly appreciated!
> 
> Regards,
> Jason
> 
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/best-way-to-handle-multiple-buttons-in-a-form--tp15151826p15471656.html
Sent from the Struts - User mailing list archive at Nabble.com.


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