39

有没有办法将页面从 Java 方法重定向到其他页面?

我只能使用以下方式转发它:

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

或使用导航规则faces-config.xml

你有什么想法?

4

3 回答 3

69

不知道你在追求什么,但ExternalContext#dispatch()它只做转发,而不是重定向。你想ExternalContext#redirect()改用。

externalContext.redirect("foo.xhtml");

甚至是外部的(这在调度中是不可能的)

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

您通常希望在 bean 的 action 方法中执行此操作。


由于您在评论中提到了 JavaScript,因此您可以使用 JS 进行重定向:

window.location = "foo.xhtml";
// Or
window.location = "http://stackoverflow.com";
于 2011-05-10T21:42:10.783 回答
2
FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "page.xhtml");

效果也一样。

于 2014-06-19T10:42:34.030 回答
-2

请尝试调用以下静态函数:

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

FacesContext.getCurrentInstance().getExternalContext().redirect(url);
于 2018-02-19T20:36:40.203 回答