MRX
MRX

Reputation: 1651

Jsf2.0 preRenderView

i am using jsf 2.0

i have question accoring to PreRenderView.

in my Bean i have method like

public void init() throws Exception
    {

        FacesContext.getCurrentInstance().getExternalContext().redirect("/To My Page");
        if(!FacesContext.getCurrentInstance().isPostback())
        {
            System.out.println("Kshitij");
        }
    }

when this method is executing it is also printing "Kshitij" in servler log.

then redirecting to page.

why? i think it has to redirect to page first.

Upvotes: 0

Views: 715

Answers (1)

BalusC
BalusC

Reputation: 1108632

Why do you think that the actual redirect is performed first? The method has to finish running first before the server can ever continue the control over the request/response. It's impossible to pause code execution halfway and then continue code execution at exactly the same location in a brand new request and thread.

The redirect() call basically sets the Location response header. Only when the method has returned, the server will send the response and then the browser will send a new request on that location.

Add a return statement or an if/else if you want to skip the printing when you need to redirect.

if (youNeedToRedirect) {
    FacesContext.getCurrentInstance().getExternalContext().redirect("/To My Page");
}
else {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        System.out.println("Kshitij");
    }
}

This all has got nothing to do with JSF or preRenderView. It's all just basic Java and HTTP.

Related:

Upvotes: 1

Related Questions