Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to dynamically set the action attribute

 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, this is not the way.

Instead do:

JSF

JS
[ October 10, 2006: Message edited by: B L Scholtz ]
 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does value1, value2, etc come from?
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
....
}
 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see http://balusc.xs4all.nl/srv/dev-jep-com.html
 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lynette. This will help greatly.
 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bauke. I'll give it a try.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic