[Logo]
Forums Register Login
How to link different buttons to different action methods
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.
Are you using a web app framework like Struts or JSF or are you using plain old JSP and Servlets?
 
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();
i'm using struts, i added
parameter="operation" to action mapping but i couldn't use it with html:button
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");
}
}
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.
A simple way to do it is with Javascript:



Hope this helps
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.
// 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.
Hi

Its better u use DispacthAction rather than lookupdispatchaction by varying the parameter attribuet of ur action element
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
 
Originally posted by Fletcher Estes:
A simple way to do it is with Javascript:



Hope this helps
 
 
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!!!
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
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 19, 2017 09:38:48.