是否有某种方法如何从Java方法重定向页面到其他页面?
我只能转发使用:
FacesContext.getCurrentInstance().getExternalContext().dispatch("/foo.xhtml");
或使用faces-config.xml
的导航规则。
你有什么想法吗?
答案 0 :(得分:60)
不确定您追求的是什么,但ExternalContext#dispatch()
仅执行转发,而不是重定向。您想改用ExternalContext#redirect()
。
externalContext.redirect("foo.xhtml");
甚至是外部(发送时无法实现)
externalContext.redirect("http://stackoverflow.com");
您通常喜欢在bean的动作方法中执行此操作。
由于您在评论中提到了JavaScript,因此以下是使用JS重定向的方法:
window.location = "foo.xhtml";
// Or
window.location = "http://stackoverflow.com";
答案 1 :(得分:0)
FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "page.xhtml");
同样适用。
答案 2 :(得分:-2)
请尝试调用以下静态函数:
String url = "/meta/default/inbox"; // Your URL here
FacesContext.getCurrentInstance().getExternalContext().redirect(url);