File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to handle mutiple buttons

 
Chitti pokala
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chitti pokala
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please give me some examples or websites where I can find more information on this.....
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also u need this in JSP

<input type="hidden" name="method" value="save"/>
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to have different hidden fields for different buttons or only one hidden field for all the buttons.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
sudhakar Tadepalli
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Kerry Wilson
Ranch Hand
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac Ruby VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but you know some people just love to use tags when not necessary.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic