• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when is the backing bean instantiated

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am new to jsf so if this seems like a very basic question i do apologize.
when is the beacking bean of a jsp instantiated. i mean when are the values set using the setter methods. is it at the time of page loading , page submitting???
if i write a javascript say
function()
{

document.getElementbyId["somefield"].value="somevalue";
}
and this function gets executed on a button click(the button does only this, page is not submitted or anything), will the corresponding setSomeField() method be executed immediately??
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you refer to a JSF View that references a previously-undefined backing bean, the framework will instantiate the bean as part of the process of preparing the view display. If it has managed properties, their setters will be called at that time. If any of the managed properties are themselved un-instantiated backing beans, those beans are first constructed and their managed properties (if any) are set.

However, thereafter the setter and getter methods may be invoked many times on a View Request/submit cycle. Which is why you should not attach complex logic or actions that have side-effects to the get/set methods.
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"the framework will instantiate the bean as part of the process of preparing the view display"

so does this mean that the setters will be called when the page is loaded for view?? hence if i call a javascript fucntion which resets a value of a form element without submitting the page, this value will not be available to the bean?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Walker wrote:"the framework will instantiate the bean as part of the process of preparing the view display"

so does this mean that the setters will be called when the page is loaded for view?? hence if i call a javascript fucntion which resets a value of a form element without submitting the page, this value will not be available to the bean?


That's one reason I avoid calling JSF Views "pages". Too much confusion from non-JSF/non-MVC usage.

JSF doesn't "load a page". It takes a view definition (usually from a file, often in JSP form) and compiles it into a tree structure. This structure serves as the basis for invoking the backing bean functions. When an action is invoked, a different View definition may be requested, in which case the old tree is discarded, and a new tree is compiled using the indicated new view definition.

JSF works with discrete rendering engines that take the view tree and the JEE environment (including JSF backing beans) and runs the selected renderer against that tree. The rendering process may and often will involve calling backing bean getters. Most commonly, the renderer used is the stock HTML renderer, but other renderers are also possible, including user-defined ones.

I really recommend you find some good documentation on the JSF lifecycle, and a book or 2 on JSF wouldn't be a bad investment.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic