How to perform navigation in preRenderView listener method

99封情书 提交于 2019-11-27 15:15:22
BalusC

Navigation based on a method's return value is only performed by components implementing ActionSource2 interface and providing an attribute taking a MethodExpression for that, such as action attribute of UICommand components, which is queued during Apply Request Values phase and invoked during Invoke Application phase.

The <f:event listener> is merely a component system event listener method, not an action method. You need to perform the navigation manually as follows:

public void performWeakLogin() {
    // ...

    FacesContext fc = FacesContext.getCurrentInstance();
    fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "mainPortal");
}

Alternatively, you can also send a redirect on a given URL, which is more useful for the case you don't want to navigate internally, but externally:

public void performWeakLogin() throws IOException {
    // ...

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/stdPortal/stdPages/mainPortal.xhtml");
}

Unrelated to the concrete problem, a servlet filter is a better place for the job of performing request based authorization/authentication.

See also:

I use JBoss 7 with JSF 2.1. The solution from BalusC was redirecting to the JBoss default error page, even though I had already set the default error page in the web.xml‎‎

<error-page>
    <error-code>404</error-code>
    <location>/myapp/404.xhtml</location>
</error-page>

To redirect to my own error page, I used the response to send the error:

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse();
try {
    response.sendError(404);
} catch (IOException ioe) {
    ioe.printStackTrace();
}
facesContext.responseComplete();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!