wood burning stoves 2.0*
The moose likes JSF and the fly likes when is the backing bean instantiated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "when is the backing bean instantiated" Watch "when is the backing bean instantiated" New topic
Author

when is the backing bean instantiated

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
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

Joined: Jun 25, 2001
Posts: 15952
    
  19

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.


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

Joined: Sep 23, 2009
Posts: 87
"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

Joined: Jun 25, 2001
Posts: 15952
    
  19

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.
 
wood burning stoves
 
subject: when is the backing bean instantiated
 
Similar Threads
Display Different Sections while selecting different radio buttons
Where is doGet() in JSF?
JSF: Bean Instantiation
display message when page is first displayed
JSP - Java and Javascript 'for' loop problem