40

Is there some way how to redirect page to other page from Java method?

I'm able only to forward it using:

FacesContext.getCurrentInstance().getExternalContext().dispatch("/foo.xhtml");

or using navigation-rules of faces-config.xml.

Do you have any ideas?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gaffcz
  • 3,469
  • 14
  • 68
  • 108
  • What sort of "easy" way are you thinking of? – Ali May 10 '11 at 19:23
  • I don´t know:) Actually I need any way – gaffcz May 10 '11 at 19:25
  • Well, you've already mentioned the navigation-rules method. Another is to send a Javascript snippet from Java telling the browser to navigate (`document.location.href="someplace"`). You can send a snippet using `JavscriptContext.addJavascriptCall(..., ...)` but it's a lousy method :) – Ali May 10 '11 at 19:32
  • Oh, thank you, but for now I hope some easier method exists (i know almost nothing about javascript :-) – gaffcz May 10 '11 at 19:51

3 Answers3

72

Not sure what you're after, but the ExternalContext#dispatch() does only a forward, not a redirect. You'd like to use ExternalContext#redirect() instead.

externalContext.redirect("foo.xhtml");

or even external (which is not possible with dispatch)

externalContext.redirect("http://stackoverflow.com");

You'd normally like to do this in bean's action method.


Since you mentioned JavaScript in the comments, here's how you could redirect using JS:

window.location = "foo.xhtml";
// Or
window.location = "http://stackoverflow.com";
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you BalusC, that's answer to the question how to redirect page from java bean. And rest of my answer (any to any) is bullshit :) – gaffcz May 11 '11 at 07:15
  • 1
    I've learned more about JSF from @BalusC than from any other source. Thanks for sharing, man. I owe you a beer if we ever run in to each other. – fusion27 Nov 24 '14 at 13:32
  • Is the URL you're redirecting to relative to the context path or to the path of the bean's calling facelet? – Wecherowski Jan 12 '17 at 01:19
  • 1
    @Wecherowski Relativity doesn't change the subject. The new URL is just relative to current URL not to path or package or bean or whatever doesn't represent the current URL. You may find this related Q&A more helpful: http://stackoverflow.com/q/30683584 – BalusC Jan 12 '17 at 07:46
2
FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "page.xhtml");

Works just as well.

mcrius
  • 49
  • 1
-2

please try with the call to the following static function:

String url = "/meta/default/inbox"; // Your URL here

FacesContext.getCurrentInstance().getExternalContext().redirect(url);
Daniel Arthur
  • 456
  • 7
  • 17
  • Couple of things...1: The spaces in your code make it fail, 2: it is 'identical' with what is done in the lots of upvoted answer with a very small difference, the external context is assigned to a variable there. Hence all the downvotes I think. Better to remove the question. Cheers – Kukeltje Feb 20 '18 at 09:56