This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to link different buttons to different action methods

 
Deli Dumrul
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a web app framework like Struts or JSF or are you using plain old JSP and Servlets?
 
Varun Khanna
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Deli Dumrul
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm using struts, i added
parameter="operation" to action mapping but i couldn't use it with html:button
 
Sham Grandhe
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple way to do it is with Javascript:



Hope this helps
 
Deli Dumrul
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Its better u use DispacthAction rather than lookupdispatchaction by varying the parameter attribuet of ur action element
 
Sham Grandhe
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fletcher Estes:
A simple way to do it is with Javascript:



Hope this helps
 
Ankit Pradhan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic