aspose file tools*
The moose likes Struts and the fly likes Handling buttons with same names in two forms using a single Action class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Handling buttons with same names in two forms using a single Action class." Watch "Handling buttons with same names in two forms using a single Action class." New topic
Author

Handling buttons with same names in two forms using a single Action class.

Nivan scorp
Greenhorn

Joined: Dec 06, 2004
Posts: 23
Hi,
I have 2 forms with the same button value i.e continue.In Scenario A, the click of the button should call method edit(),and in Scenario B, the click of the button should call method verify().
I have written an Action class that subclasses LookupDispatchAction.

My problem is that in my getKeyMethodMap() method of my Action, I am
trying unsuccessfully to map the method names to the button value.

This is how my getKeyMethodMap method looks like:

-----------------------------------------------------------
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("setup", "setup");
map.put("scenarioA.continue", "edit");
map.put("scenarioB.continue", "verify");
return map;
}

My property file has these entries.
Setup=setup
Verify=verify
scenarioA.continue= continue
scenarioB.continue= continue
---------------------------------------------------------------------

Can anyone suggest how I can achieve this in Struts?


MY RESOLUTION:
I resolved the issue by mapping both of these to a method called getCorrectMethod where I checked for the value of a hidden field to determine which method to invoke.
-------------------------------------------------------
Code:
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("setup", "setup");
map.put("scenarioA.continue", "getCorrectMethod");
map.put("scenarioB.continue", "getCorrectMethod");
return map;
}


public ActionForward getCorrectMethod (
ActionContext context,
ActionMapping mapping,
BaseActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws ServiceFrameworkException, ActionFrameworkException {

String nextScreen = request.getParameter("screenName");
if (nextScreen.equals("ScenarioA")) {
return edit(context, mapping, form, request, response);
} else if (nextScreen.equals("ScenarioB")) {
return verify(context, mapping, form, request, response);
}
}
--------------------------------------------------------


But, I need to achieve this without using hidden fileds. DOes anyone have
a better resolution to t he p[roblem I am facing? It would be of immense help.

Thanks,
Scorp N.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Scorp,

Check out my solution here:
http://marcpeabody.com/commanddispatch.html

These classes allow you to specify the Action method in the property attribute of the struts button like so:
your page 1 -
<html:submit property="command(edit)" value="Continue">
your page 2 -
<html:submit property="command(verify)" value="Continue">

OR if you want to use regular html instead of html:submit...
your page 1 -
<input type="submit" name="command(edit)" value="Continue">
your page 2 -
<input type="submit" name="command(verify)" value="Continue">

It's easy to use. If you have any questions, feel free to send a Private Message my way.

Cheers!


A good workman is known by his tools.
Nivan scorp
Greenhorn

Joined: Dec 06, 2004
Posts: 23
hey Mark,
Thanks for your valuable suggestion. Its been of great help.

Regards,
Nivan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling buttons with same names in two forms using a single Action class.