aspose file tools*
The moose likes JSF and the fly likes Is there a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Is there a "page load" type of event?" Watch "Is there a "page load" type of event?" New topic
Author

Is there a "page load" type of event?

Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
I come from an ASP.NET background and there was an event there called page load. It would be fired before the page actually rendered to the browser. I'm wondering if there is something similar in JSF. I'd like to check something in the database before displaying the initial page (index.jsf) to the user. Possible?
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

why don't you do it in constructor of the bean itself. Anyways whenever a page is created its corresponding bean is also created.


My Blog SCJP 5 SCWCD 5
venkatesh babu
Greenhorn

Joined: Sep 20, 2008
Posts: 6
Take a look at the JSF lifecycle... The renderResponse phase is the last phase which is invoked when the page is being displayed. Usually the getter methods of your JSF components are invoked in this phase.. So, you could probably put in stuff you want in them.. But if the RI you are using is not good enough, the getter methods may be invoked during the validation or conversion phases too.. So, it may be useful to see which phase you are in by checking the phase id before executing the code..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

"venkatesh mr", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Indeed just use the bean's constructor or initialization block. You can also set it as a managed property in the faces-config.xml. Since JSF 1.2 you can also create a method with the @PostConstruct annotation, that method will be fired when the bean is created and all of its managed properties are been set. In JSF you can also make use of the 'beforePhase' and 'afterPhase' attributes of the f:view tag which you bind to a method which takes a PhaseEvent argument. In there you can check the current PhaseId and handle accordingly.

Originally posted by Himanshu Gupta:
why don't you do it in constructor of the bean itself. Anyways whenever a page is created its corresponding bean is also created.


Only if the bean is request scoped -which is the right design approach indeed.

Originally posted by venkatesh mr:
But if the RI you are using is not good enough, the getter methods may be invoked during the validation or conversion phases too.. So, it may be useful to see which phase you are in by checking the phase id before executing the code..


So you're saying that this is due to the RI? You're wrong. And you're also wrong to suggest to place this kind of logic in the getters. Getters are solely there to return data. You however can consider introducing lazy loading in getters, e.g. if (data == null) { loadData(); } return data; so that it won't be executed more than once in bean's life.
[ September 20, 2008: Message edited by: Bauke Scholtz ]
venkatesh babu
Greenhorn

Joined: Sep 20, 2008
Posts: 6
Thanks Chris.. Yeah.. even though the approach is not clean enough, you might sometimes want to do some stuff after the JSF life cycle has come to the renderResponse phase after doing some processing in the converters and validators.. I dont think you can put that kind of code in the bean's constructors.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

The problem with JSF is that there's not a completely straightforward relationship between page loads and bean construction. A request-scope bean gets constructed "EVERY" time the page (re)displayed, and since JSF is more like a GUI than a traditional HTTP page view, the same page can be displayed multiple times - especially if the user enters invalid data.

A Session-scope backing bean is also a problem, since only the first time the page is displayed for that user will it be constructed, and if you come back to the page from some other page, the bean already exists.

There is supposed to be a way to do this, and I was hoping someone would remember it. Try searching this forum, probably within the last 6 months.


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

Joined: Apr 16, 2009
Posts: 7
options

1. http://webmoli.com/2008/11/10/jsf-onload-action-during-restore-and-render-response-phase/
2. if your using jsf ibm RAD hx:scriptCollector preRender
3. implements a PhaseListener
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a "page load" type of event?
 
Similar Threads
Not a Frequent Exception
Enable isn't working in script
display data to jsp page during page load event (with Spring and Acegi security)
refreshing problem using java script in internet explorer
onBeforeLoad event not triggered