GeeCON Prague 2014*
The moose likes Struts and the fly likes How to link different buttons to different action methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to link different buttons to different action methods" Watch "How to link different buttons to different action methods" New topic
Author

How to link different buttons to different action methods

Deli Dumrul
Greenhorn

Joined: Jul 06, 2004
Posts: 9
Hi,

I have records listed in a jsp page. Near each record, there are two buttons, edit and delete. How can i link these buttons to appropriate action methods.

Thanks.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Are you using a web app framework like Struts or JSF or are you using plain old JSP and Servlets?


GenRocket - Experts at Building Test Data
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Deli Dumrul:
Hi,

I have records listed in a jsp page. Near each record, there are two buttons, edit and delete. How can i link these buttons to appropriate action methods.

Thanks.


Well if it's JSF
just bind your "action" attribute with the backing bean's action method
such as
<h:commandButton ... action="#{pc_beanName.actionMethod}"/>
and if it just simple JSP/Servlet, you can call a javascript function on the button and smiply call
document.form.action=......
document.form.submit();


- Varun
Deli Dumrul
Greenhorn

Joined: Jul 06, 2004
Posts: 9
i'm using struts, i added
parameter="operation" to action mapping but i couldn't use it with html:button
Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
Hi Deli Dumrul,
if your using struts then you should try with this. we have a class named LookupDispatchAction in the package org.apache.struts.actions(not action its actions). here you need give the method name as that of the button and it should be through the ApplicationResource.properties file the method signature is same as that of the execute method.


public class MoreSubmitsAH extends LookupDispatchAction {
public MoreSubmitsAH() {
}
protected Map getKeyMethodMap() {
/**@todo Implement this org.apache.struts.actions.LookupDispatchAction abstract method*/
Map map = new HashMap();
map.put("button.add","add");//the 2nd string is the method name;
map.put("button.del","delete");
map.put("button.update","update");
return map;
}
public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
return mapping.findForward("add");
}

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
return mapping.findForward("del");
}

public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
return mapping.findForward("update");
}
}
Deli Dumrul
Greenhorn

Joined: Jul 06, 2004
Posts: 9
Hi kumar,
if i understood correctly my action class should extend LookupDispatchAction and implement getKeyMethodMap. but i couldn't find ApplicationResource.properties file. i am extending the struts-blank.war so may be i have to create an ApplicationResources.properties file. Thank you very much for your detailed reply.
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
A simple way to do it is with Javascript:



Hope this helps
Deli Dumrul
Greenhorn

Joined: Jul 06, 2004
Posts: 9
Hi, i changed my action class to extend LookupDispatchAction and created the ApplicationResources file with the needed entries. in the jsp i have this line

<html:button property="action" value="add" />

when i click the add button nothing happens. any ideas?

thanks.
Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
// jsp file
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>

<html:form method="post" action="/moreSubmitsAction">
<table width=60%>
<tr>
<td>
<html:submit property="work"> //property name work same for all the buttons
<bean:message key="button.add"/>
</html:submit>
</td>

<td>
<html:submit property="work"> //property name work
<bean:message key="button.del"/>
</html:submit>
</td>

<td>
<html:submit property="work">//property name work
<bean:message key="button.update"/>
</html:submit>
</td>
</tr>
</table>
</html:form>



// Add this to struts_config.xml file

<!-- Under "form-beans" tag-->
<form-bean name="MoreSubmits" type="org.apache.struts.action.DynaActionForm" />


<!-- under "action-mappings" tag -->
<action path="/moreSubmitsAction" name="MoreSubmits" type="src.MoreSubmitsAH" parameter="work">
<forward name="add" path="/jsp/Add.jsp" />
<forward name="del" path="/jsp/Delete.jsp" />
<forward name="update" path="/jsp/Update.jsp" />
</action>



// Add this to ur ApplicationResource.xml file

#Messages for More_Submits
button.add=Add Button
button.del=Delete Button
button.update=Update Button



plz use the previous code for the action class with this code. check the button names that is the property should be same for

all the buttons. and the names of the methods must match the name in the map. ok hope you could go on with this Deli.
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Hi

Its better u use DispacthAction rather than lookupdispatchaction by varying the parameter attribuet of ur action element
Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
Hi Sreenath,
Iam not clear with what you can you explain. that in a more detailed manner. if possible plz provide and example. by varying the parameter attribuet of ur action element what do you exactly mean by that.

sham
Ankit Pradhan
Greenhorn

Joined: Jan 05, 2005
Posts: 16
Originally posted by Fletcher Estes:
A simple way to do it is with Javascript:



Hope this helps
Ankit Pradhan
Greenhorn

Joined: Jan 05, 2005
Posts: 16
Originally posted by :


Hope this helps<hr></blockquote>[/QB]



hi i tried the above code to call the "action-mapping" which i have specified in the struts-config... but it is giving an error at the line(document.forms[0].submit() ... it is saying that "Object doesn't support this property or method" .... can somebody help me in knowing what does that mean...
the codes stucks at submitt and doesnt take it to the JSP which is mentioned in <action-mapping>
Please help!!!
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
The only way I've seen to have multiple submits for a single form (at least when I looked on the struts site, maybe this is has been changed). Was to fake it by having the different buttons change the value of a hidden property (using javascript) and then inside the action class you check this property (with an if or switch statement).


-Tad
 
GeeCON Prague 2014
 
subject: How to link different buttons to different action methods