This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSF and the fly likes Setting the Action for a CommandButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Setting the Action for a CommandButton" Watch "Setting the Action for a CommandButton" New topic

Setting the Action for a CommandButton

Eric Bresie

Joined: Apr 05, 2011
Posts: 20
I am trying to dynamically set the action of a h:commandButton based on a selected selectOneMenu item (which is populated by database items).

I have a change listener to adjust the managed page beans state to help determine the "next page". The commandButton's action is set on the "next page". My assumption was the value of the "next page" would "dynamically replace" the actual action value, but it keep the original value set initially.

Excerpt of the page:

Excerpt of the bean looks like:

I suspect I need some kind of (i.e. update, render, p:ajax) of some type on the selectOneMenu , but I'm still confused in it's usage and the different ways of dealing with "listeners" (i.e. JSF 1.x listener vs JSF 2.x ajax ).

Help is appreciated.

K. Tsang

Joined: Sep 13, 2007
Posts: 2380

I don't understand how you going to update the action of the h:commandButton because this action points to a method in the backing bean. What this method does is up to you inside the bean. You mentioned the selected value from the selectOneMenu changes which page to go then as long as the bean knows the selected value, you will be fine.

Under this situation, your selectOneMenu is fine. Everything is done in the backing bean. So when testing once the select menu changes value, display this value to confirm.

Eric Bresie

Joined: Apr 05, 2011
Posts: 20
The backing bean gets the value from the select One, and updates the "nextPage" propertu, but the resulting rendered HTML still shows the original action not the new bean property value. So if I have items ("one", "two", "three") with a default of "home", and select "one", then backing bean updates the "nextPage" property to be "one", but rendered still shows the equivalent "home" view/page. I tried having a "next()" method (instead of a 'getter/setter" method) as well which uses the nextPage bean property and this didn't work either.
Eric Bresie

Joined: Apr 05, 2011
Posts: 20
After further investigation, it appears my problem was due to a navigation rules I had previously defined in the faces-config.xml when I was working in a JSF 1.x setup but now I am working in JSF 2.x. After removing these, it navigated to the correct view per the properties value.
jQuery in Action, 2nd edition
subject: Setting the Action for a CommandButton