*
The moose likes Struts and the fly likes how to handle mutiple buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to handle mutiple buttons" Watch "how to handle mutiple buttons" New topic
Author

how to handle mutiple buttons

Chitti pokala
Ranch Hand

Joined: Jan 11, 2005
Posts: 46
Hi
I have four different buttons on the JSP: Save, Submit, Return and Close.
These four buttons will save different data to the database.

Then how to write my action classes. Should I write seperate action class for every button
OR
One main action class and seperate methods for every button.

If so how to do this.

Please let me know.

Thanks in Advance.
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
Extend org.apache.struts.actions.DispatchAction, set parameter to method (or whatever) in struts-config.xml.

Html Submit buttons should be named method (or whatever) in the page.

When Struts processes it will call the appropriate method in the Action, methods should use same signature as execute, but, name them save, reset or whatever.


http://www.goodercode.com
SCJP 1.4
Chitti pokala
Ranch Hand

Joined: Jan 11, 2005
Posts: 46
can you please give me some examples or websites where I can find more information on this.....
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
And also u need this in JSP

<input type="hidden" name="method" value="save"/>
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
You'r action class :

public class LifeAActionbk extends DispatchAction {

public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

return mapping.findForward("list");
}


public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {


return mapping.findForward("edit");
}


}

struts-configfile

<action path="/edit" type="com.test.web.actions.LifeAAction"
name="dataForm" scope="session" parameter="method" validate="true" input="page.lifeA">
<forward name="edit" path="page.edit" />
</action>
Chitti pokala
Ranch Hand

Joined: Jan 11, 2005
Posts: 46
I have to have different hidden fields for different buttons or only one hidden field for all the buttons.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Do not go the route of hidden fields to handle this.

A popular method of handling multiple button isdescribed here with a link to LookupDispatchAction's javadoc, which has very good documentation.
[ August 12, 2005: Message edited by: Marc Peabody ]

A good workman is known by his tools.
sudhakar Tadepalli
Ranch Hand

Joined: Dec 27, 2001
Posts: 130
Mark may be right but I just followed Matt Raible "Equinox" application as a model. He used Hidden files in his jsp's.
michelle Wang
Ranch Hand

Joined: Jan 12, 2004
Posts: 65
Hi Sudhakar, do you set the value of the hidden field using javascript? I had been having problem with using javascript with struts...Could you please provide sample code?
[ August 12, 2005: Message edited by: michelle Wang ]

michelle Wang <br />SCJP 1.4
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
If you have multiple buttons named the same thing whichever button you press will be submitted to the server.

Example:

<input type="submit" name="method" value="Add"/>
<input type="submit" name="method" value="Edit"/>
<input type="submit" name="method" value="Delete"/>

if the Add button is clicked method=add is submitted to the server

I think if you put <html:submit property="method" value="Add"/>
that should work.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Kerry Wilson:
If you have multiple buttons named the same thing whichever button you press will be submitted to the server.

Example:

<input type="submit" name="method" value="Add"/>
<input type="submit" name="method" value="Edit"/>
<input type="submit" name="method" value="Delete"/>

if the Add button is clicked method=add is submitted to the server

I think if you put <html:submit property="method" value="Add"/>
that should work.


It doesn't matter if you use the Struts tag or not. It should work either way.
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
Yeah, but you know some people just love to use tags when not necessary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to handle mutiple buttons