user517491
user517491

Reputation:

Redirect a user to a different page from f:event listener method

I have following code for initializing bean values depending upon url parameter.

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

This is working fine. But I want that in certain conditions(some validation forexample), the user is redirected to another page from listener method.

How can I do that?

Upvotes: 4

Views: 4443

Answers (1)

BalusC
BalusC

Reputation: 1108722

Use ExternalContext#redirect().

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

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

Upvotes: 4

Related Questions