Search code examples
jsfjsf-2navigationlifecycle

Does the JSF lifecycle reapply to the outcome Facelet when I navigate?


I have a simple login.xhtml facelet which contain a username and password input and a command button. The command button simply have an action="welcome" outcome which display the welcome.xhtml facelet.

I'm very new to JSF. As I read the JSF lifecycle, when i click the button then the invoke application lifecyle handle the navigation and render(??) the welcome.xhtml. I'm still confuse if the welcome.xhtml will start a new facelet lifecycle or not.

PS: I notice that the url doesn't change to welcome.xhtml. Does the jsf lifecycle bound to the request?


Solution

  • Yes, the JSF lifecycle is request-bound. And yes, the outcome will by default render in the same request.

    The key which is confusing you is likely "forward" versus "redirect" in terms of HTTP servlet requests.

    JSF by default forwards the request to the target page. If you know the Servlet API well, you'll understand that it is then under the hoods doing the following:

    request.getRequestDispatcher("welcome.xhtml").forward(request, response);
    

    This way the target page has access to the same request object. If the target page is different from the page where the form is been submitted to (the login.xhtml actually), then you won't see the change being reflected in the browser address bar.

    You can however configure JSF to redirect the request to the target page by adding a <redirect/> to the navigation case, or by calling ExternalContext#redirect() in bean's action method, or in a nice JSF 2.0 way by adding faces-redirect=true parameter to the button's action:

    <h:commandButton value="login" action="welcome?faces-redirect=true" />
    

    Either way, it is in Servlet API terms doing the following:

    response.sendRedirect("welcome.xhtml");
    

    which basically instructs the client to fire a brand new GET request to the given location. Note that this way any request- and view scoped beans of the initial request will be trashed and recreated.