Redirect user to another page from f: event listener method

I have the following code to initialize bean values ​​depending on the url parameter.

<f:metadata>
    <f:viewParam name="id" value="#{inningBean.inningId}" />
    <f:event type="preRenderView" 
        listener="#{inningBean.initInningBeanForBallByBallScoring}" />
</f:metadata>

It works great. But I want that in certain conditions (for example, to check the correctness), the user is redirected to another page from the listener method.

How can i do this?

+3
source share
1 answer

Use ExternalContext#redirect().

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

    if (someCondition) {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.redirect(ec.getRequestContextPath() + "/other.xhtml");
    }
}
+3
source

All Articles