• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a "page load" type of event?

 
Chris Stewart
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't you do it in constructor of the bean itself. Anyways whenever a page is created its corresponding bean is also created.
 
venkatesh babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"venkatesh mr", please check your private messages for an important administrative matter.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
allen catadman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic