File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Managed bean is immediately called on page load Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Managed bean is immediately called on page load" Watch "Managed bean is immediately called on page load" New topic
Author

Managed bean is immediately called on page load

Todor Molev
Greenhorn

Joined: Jun 09, 2012
Posts: 2
Hi guys,
I've got very simple code which only test if the managed bean method is called on button click.
Is this kind of a bug that my managed bean method is being called every time the page is loaded?


... and the bean:



I am using Netbeans 7.1.1 with tomcat 7.0.22 and JSF 2.1
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Welcome to the JavaRanch, Todor!

There are a couple of factors involved here.

First, JSF is based on the concept of "postback". Unlike other frameworks, where a URL loads a page and that's it, JSF presents a View as a page and keeps presenting it until instructed to navigate to some other View. So the same "page" may be loaded multiple times - once for each postback operation.

Secondly, J2EE Request scope is based on page load requests. So each postback generates a new page load request, even though the same View is being presented. Request scope objects are created to support the request and then destroyed at the end of the request, so each update of the View causes an entirely new request bean to be created and destroyed - no state data (property values) carry between requests and it's all rebuilt from the beginning each time. Request scope is actually mostly useless in JSF.

Finally, JSF is based on a well-defined lifecycle where the Controller references the Model to create a View using a Model, accept data input from a View, validate the incoming data, update the Model from the View (IF and ONLY IF every data item passes validation). And in the case of Request-scope - destroy the updated Model.

To do all of the above, the set/get methods of the Model object (backing bean) properties may be called many times, since the Model is the official copy of the data - JSF doesn't keep internal copies. And for that reason, multiple calls to set/get methods are not a bug, but an inherent part of how the JSF lifecycle works. Which means that get/set methods should avoid having side-effects and should definitely avoid doing high-overhead operations.


Customer surveys are for companies who didn't pay proper attention to begin with.
Todor Molev
Greenhorn

Joined: Jun 09, 2012
Posts: 2
Thank very much for the exhaustive answer, Tim.
It was awkward for me that when my application gets deployed and the view is rendered for the first time I got the bean method, given as a value to the "onclick" attribute, called.
I redeployed a couple of times and this strange behaviour disappeared.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Managed bean is immediately called on page load