tags:

views:

110

answers:

1

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 the the url doesn't change to welcome.xhtml. Does the jsf lifecycle is bound to the request?

Thank you

+1  A: 

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.

BalusC
but if the welcome.xhtml is never displayed before and does jsf need to do the restore view lifecycle for the welcome facelet?. If so, is that it need to start a difference lifecycle ?
robinmag
The restore view phase will always be invoked regardless of if there's a view or not. If there's none, then JSF will just create a new one during this phase. Although targeted on JSF 1.2, the following article applies on JSF 2.0 as well, you may find it useful: [Debug JSF lifecycle](http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html).
BalusC
thank you ! ...
robinmag
You article is great. Recommended for every jsf newbie :)
robinmag