Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF life cyclus questions

 
Vladislav Simovic
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct!!
 
Vladislav Simovic
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vladislav Simovic
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic