Author
how to dynamically set the action attribute
Joseph Siao
Ranch Hand
Joined: Feb 26, 2004
Posts: 32
I have a commandbutton declared as: <af:commandButton text="#{res[\'button.delete\']}" id="cmdDelete" action="" on_click="return confirmAction(this)"/> Then I have a javascript for the confirmAction: function confirmAction(obj){ obj.action = "#{backingTest.testMethod}"; return true; } This doesn't seem to set the action. Am I doing it wrong?
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted Oct 10, 2006 00:53:00
0
No, this is not the way. Instead do: JSF JS [ October 10, 2006: Message edited by: B L Scholtz ]
Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
Joseph Siao
Ranch Hand
Joined: Feb 26, 2004
Posts: 32
Thank you for the answer. But I need to set the action dynamically. I will be having 4 conditions, and each condition will call a different method on my backing bean. Something like this: function confirmAction(obj){ if(value1){ obj.action = "#{backing.method1}"; return true; }else if(value2){ obj.action = "#{backing.method2}"; return true; }else if(value3){ obj.action = "#{backing.method3}"; }else { return false; } } suggestions please.
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted Oct 10, 2006 04:33:00
0
Where does value1, value2, etc come from?
Richard Green
Ranch Hand
Joined: Aug 25, 2005
Posts: 536
cant you have one method in your backing bean that looks at value1,value2... and calls the appropriate method? h:commandButton value="submit" action="#{myBean.action}" on_click="validate();" /> public String action(){ if (value1) return method1; if (value2) return method2; .... }
MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Joseph Siao
Ranch Hand
Joined: Feb 26, 2004
Posts: 32
Thanks again. value1, value2, and value3 will be coming from a modal dialog. Its values are yes, no and cancel. depending on the value, different methods are called from a single button. is this possible? or is there a different approach?
Joseph Siao
Ranch Hand
Joined: Feb 26, 2004
Posts: 32
Thanks Lynette. I can do that. but the values are coming from the jsp page. or is there a way to pass a parameter through the action? something like this: h:commandButton action="#{myBean.method(parameter)}"
Richard Green
Ranch Hand
Joined: Aug 25, 2005
Posts: 536
see http://balusc.xs4all.nl/srv/dev-jep-com.html
Joseph Siao
Ranch Hand
Joined: Feb 26, 2004
Posts: 32
Thanks Lynette. This will help greatly.
Joseph Siao
Ranch Hand
Joined: Feb 26, 2004
Posts: 32
Hi Lynette. I tried the website you gave me. I used attribute on my commandButton to pass values. I have a new error, attribute does not support runtime expressions. Here is how I did it: h:attribute param="response" value="<%=resp;%>" any way around this?
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted Oct 10, 2006 22:57:00
0
Put it in the sessionMap or requestMap using JSP . And then retrieve the value from FacesContext.getCurrentInstance().getExternalContext().getSessionMap() or .getRequestMap().
Joseph Siao
Ranch Hand
Joined: Feb 26, 2004
Posts: 32
Thanks Bauke. I'll give it a try.
subject: how to dynamically set the action attribute