File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 23
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: 3132

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.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Eric Bresie

Joined: Apr 05, 2011
Posts: 23
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: 23
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.
I agree. Here's the link:
subject: Setting the Action for a CommandButton
It's not a secret anymore!