File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes how to dynamically set the action attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "how to dynamically set the action attribute" Watch "how to dynamically set the action attribute" New topic
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
No, this is not the way.

Instead do:

JSF

JS
[ October 10, 2006: Message edited by: B L Scholtz ]
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
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to dynamically set the action attribute