Dear all,
 
I've been investigation a little about how to call different actions from 1
form with multiple buttons. Here's a possible solution, what do you think
about it and/or do you see any possible problems?
 
The way I did it was to add a Dispatcher class, which is called from the
form, and which will call the correct action depending on which button has
been pressed. 
 
<form:form action="dispatcher.do"
 name="customForm"
 type="custom.CustomForm"> 
 
<form:submit value="Button 1" property="SUBMIT_ButOneAction.do" />
<form:submit value="Button 2" property="SUBMIT_ButTwoAction.do" />
 
We give each button a name, starting with SUBMIT_xxx. This xxx will be the
name of the mapping we will have to use for the action to be called. 
 
***Little remark: Make always sure that when having more then 1 form with
multiple buttons, to give the action and actionForm in the <form:form> tag a
different name, and in struts-config make sure that there are 2 different
mappings, with 2 different names and 2 different actionForms, but both going
to the same Digester class which always controls the forwards to Action
Classes. 
 
In this dispatcher class you can retrieve the button pressed, by going
through the request parameters, and then extracting the name of the mapping
from the parameter. (SUBMIT_ButOneAction.do becomes ButOneAction)
 
The next step is to find the Action instance that has been created by the
ActionServlet for a mapping. The problem here is that the "actions" HashMap,
stored in the ActionServlet, has been defined as protected, so the way to
solve this is to create an own ActionServlet, with a getAction() function
which will return the correct action.
 
Now, you can call the perform() method on this action, which will return an
ActionForward. (You can also go via struts-config and find the correct
mapping, like you normally would do with only one button, but my way of
working, calling the perform() method directly, is -I believe- faster, so
better performance).
 
Your comments please! Who sees any problems?
 
 
Thanks,
 
Koen
 
P.S.: When I asked some weeks ago how to solve the problem of multiple
buttons in 1 form, people suggested to use JavaScript. We tried that and it
works, but the bad thing is you still rely on the JavaScript interpretation
on the clientside and in the way I suggested above, it's more JSP / Struts
related.
 
 

Koen De Smet
The Capital Markets Company
T   +32 10 48 91 11
F   +32 3 740 10 01
Mobile   +32 497 51 88 51 
e-mail    mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>  
web    http://www.capco.com/ <http://www.capco.com/>  

 

************************************************************************
The information in this email is confidential and is intended solely
for the addressee(s).
Access to this email by anyone else is unauthorised. If you are not
an intended recipient, you must not read, use or disseminate the
information contained in the email.
Any views expressed in this message are those of the individual sender,
except where the sender specifically states them to be the views of
The Capital Markets Company.

http://www.capco.com
***********************************************************************

Reply via email to