This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes JSF life cyclus questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF life cyclus questions" Watch "JSF life cyclus questions" New topic
Author

JSF life cyclus questions

Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
I have been reading about JSF cycles, about 6 phases that it consists of, and there are some things that are not really clear to me.

Lets say we have a managed bean Color put into into request scope. Backing class for this bean is really simple:


Ok page that presents this is also very basic, it has only one field, where you put the name of color:


My question is - when is the bean Color created? Is it created after the firstForm is submitted, or before that - when you send page to client for viewing?
Bishal Ghimire
Greenhorn

Joined: Feb 01, 2011
Posts: 26

1st Phase is Restore view phase.
So, whatever default value you have in the bean gets appeared in the form by default. That means, your bean is created long before form is submitted.
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
so if i understood you correctly, every time i press the button "New color name", i wouldnt be creating a new request scoped bean color, instead that been already exists, and is just populated with new values?
Bishal Ghimire
Greenhorn

Joined: Feb 01, 2011
Posts: 26

Correct!!
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Ok, but there are still some problems with this. If I modify request to be an ajax request, like this:





First time i press the button "New color name", it will change its name to blue. But when i press it again, it will not change it back to red again. It will stay blue. Its like every time i issue ajax request, the bean is rebuilt, with its original value "red", which is then changed to "blue" in action listener. If the bean really does exist before ajax submit command, shouldnt it retain value "blue", and then change it to red in action listener, the second time I hit commandButton?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

AJAX requests are just like ordinary JSF requests except that they typically limit the parts of the form being sent and the output being received/rendered.

So in other words, each AJAX request goes through the JSF lifecycle, including the little options like "immediate=true" lifecycle short-circuits.


Customer surveys are for companies who didn't pay proper attention to begin with.
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Tim Holloway wrote:AJAX requests are just like ordinary JSF requests except that they typically limit the parts of the form being sent and the output being received/rendered.

So in other words, each AJAX request goes through the JSF lifecycle, including the little options like "immediate=true" lifecycle short-circuits.


I thought so too. So why whenever i press the request button, it looks like bean is newly created? For instance if i change bean scope to session, my example will work fine, bean is preserved during entire session, and will contain correct values.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

Request scope beans ARE newly-created on each request. AJAX doesn't change that.

Request scope means that the bean is constructed, JSF fills it in, uses it to render a page and then sends the page back to the client. Once the page is sent, the request scope bean is destroyed. AJAX requests are no different, so each AJAX request causes a new request-scope bean to be created, used, and destroyed as well.

If you want something more durable, use session scope or View scope (JSF2).
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Tim Holloway wrote:Request scope beans ARE newly-created on each request. AJAX doesn't change that.

Request scope means that the bean is constructed, JSF fills it in, uses it to render a page and then sends the page back to the client. Once the page is sent, the request scope bean is destroyed. AJAX requests are no different, so each AJAX request causes a new request-scope bean to be created, used, and destroyed as well.

If you want something more durable, use session scope or View scope (JSF2).


Thank you for your time, you are making this much easier to understand. But isnt this in contradiction, with what Bishal said that i wouldnt be creating new bean whenever i press commandButton, instead i would be just adding new values. Can you elaborate on that?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF life cyclus questions
 
Similar Threads
JSF 2.0 - Ajax not working
Validation error while trying to change a value in a request scope bean
javax.el.PropertyNotFoundException: Objetivo inalcanzable - JSF - Facelet - method-signature
It seems the code JSF 2.0 controls aren't updating backing beans
Could not navigate from one page to another !!!