1

Is it possible to have a commandButton that executes a method of a certain backing bean and then also navigates to a different page?

I know that I could return a String in the method that the commandButton calls, but the method is also used on the target-page, meaning it's often called from that same page. So for calls that come from the same page, the redirect would be unnecessary and I would like to avoid that.

The options that I have in mind right now:

  • Create a separate method for the "remote" call of the method that does the same logic and also redirects to the page
  • Use an additional h:button and use JavaScript so that if the commandButton is pressed, the h:button is pressed at the same time (Probably bad practice tho)

Any option I am missing? Is there any way to tell the commandButton itself that it's supposed to navigate somewhere or do I have to implement this in the backing-beans?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mercious
  • 378
  • 3
  • 25

2 Answers2

2

Your title and first sentence are 'dangerous' and sort of not on topic since to both, the answer is yes and you sort of describe (= answer) that in your second paragraph already yourself.

The real question further on effectively asks about conditional navigation. But let me state first that your solution of two methods is also not wrong if you just make sure you don't do actual work in the bean (which you should not).

Now conditional navigation is by itself not difficult

All basic JSF which I assume you are already aware of and this just requires something to do one or the other

So then the question remains if you can

which in turn can be used to return null or the other new page in an if/else.

Page1:

<h:commandButton action="#{mybean.action(true))" />

Page2:

<h:commandButton action="#{mybean.action(false))" />

Bean:

public String action(boolean navigate) {

    doWork();
  
    if (navigate) {
        return "page2.xhtml?faces-redirect=true";
    } else {
        return null;
    }

And if you'd want it, you could even pass null or the page name as a parameter to the method call.

Implementing detection of the source page of the action has the advantage that in the UI you do not need any knowledge on how to navigate, you always call the same method without any parameters and each new page you'd use this action on navigates to the right page without the developer needing any knowledge.

So take you pick.

Community
  • 1
  • 1
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Instead of a boolean you could also pass a String to improve it even further. – WirJun Feb 01 '19 at 07:08
  • Very extensive answer, clears up the possibilities and i actually like the Boolean way. Thanks a lot! – Mercious Feb 01 '19 at 07:13
  • @WirJun: already mentioned right below the code example. And not sure it is an 'improvement'. Subject to preference I think – Kukeltje Feb 01 '19 at 07:47
  • @Mercious: You are welcome. I hope that you also see that asking a good question is helpful and that it is often possible to narrow things down. Cheers – Kukeltje Feb 01 '19 at 07:49
-1

I'm not completely sure if I got you right, but you could do something like this:

<h:commandButton value="Click" action="otherPage.xhtml?faces-redirect=true">
     <f:actionListener binding="#{bean.method}" />
</h:commandButton>

Keep in mind that actionListener will be fired first and after that action from commandButton. Hope it helps.

Update:

Due to the fact that there was no further thinking you can use commandButton with or without redirect.

<h:commandButton value="Click" action="{bean.method}"/>
WirJun
  • 113
  • 1
  • 16