jQuery in Action, 3rd edition
The moose likes JSF and the fly likes On Load of every JSF Page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "On Load of every JSF Page" Watch "On Load of every JSF Page" New topic

On Load of every JSF Page

Shruti B

Joined: Jan 19, 2006
Posts: 7

I am new to JSF, in my application i want a few methods to be called on the load of every page can someone help me out as to how it can be done?

Regards,<br />Shruti
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17423


Welcome to the JavaRanch. We're a laid-back bunch and don't have many rules, but we really do prefer that you use a display name that has a first name and a last name and, preferably is your real name, just like you'd use it in professional correspondence. Could you change it please? Thanks.

As for your question, JSF makes itself look a little more like a traditional client/server app and less like a request/response app than other frameworks such as Struts do, so "loading a page" is a little hard to discuss (for me, anyway). Probably the closest I could come is to suggest you look at how JSF renderkits work.

An IDE is no substitute for an Intelligent Developer.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Within the JSF framework, you can try a PhaseListener

More generally, you can write a servlet filter.

There is no emoticon for what I am feeling!
Shruti B

Joined: Jan 19, 2006
Posts: 7
Hello Jeff,

I want to call a private method on load of a jsp page which uses a few objects which can be accessed by the jsp page only. So i will need a PhaseListener for the page. Without using viewId can i make a phaseListener for a specific page.

One solution that can be thought of is:

There is no init method which is executed everytime and before the JSP page loads.

All the fields declared on the jsp page happen to be execute sequentially. To simulate the onLoad method. Perform the following.

1. Under the <h:form> tag, declare a hidden variable

[Explanation: As soon as you mention the property name in the value attribute, the getter of that property from the backing bean will be called]

2. In the getter method of the property call the method which you want to call onLoad of the jsp page or write the entire code which you want to execute in the onLoad method.

This is the simplest method i could think of.

I have heard of PhaseListener on most of the forums, however i am not bale to implement it, if you could do so.. or for that matter anybody having any knowledge about this
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Have you considered overriding the jspInit metod of your page?
Rituparno Pal

Joined: Oct 10, 2004
Posts: 12
Hi Shruti
You can use the "preRender" attibute of "scriptcollector" to do this if u r on Webshere environment.

JSP Sample code:
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<hx:scriptCollector id="scriptCollector1" preRender="#{pc_MyManagedBean.onPageLoadBegin}">

The method onPageLoadBegin() should be written on the managed bean
Saskia de Jong
Ranch Hand

Joined: Jan 24, 2006
Posts: 34
Yet another trick is to create a binding to a backing bean.

setInit() will now get called at each page load. I think this is a little less of a hack than the hidden input is. (caveat you MUST also supply the getter for init even though it won't be used)

I'm also not really sure if I agree with the comment given above that page loads are something you don't want to think about. Maybe you don't want to think about them as explicit page loads, but you do want some notion of a pre-render event for the whole interface. ASP.NET looks very much like JSF and it does have this concept.

Also, if you're dealing with session scoped backing beans and need to process get parameters then there's no other way. You need some onPageLoad mechanism. Now some people would say get parameters don't belong in a JSF application. The matter of the fact is however that people wish to link to pages from external sources and when that happens there's just no way around using get parameters.
David Rissato Cruz

Joined: Jan 10, 2013
Posts: 1
I think people here want to mimetize ASP.NET PageLoad behaviour inside a ManagedBean.

In ASP.NET, the PageLoad event occurs after Components has been binded, ViewState has been loaded, and Form Values have been already updated, but before Action events are invoked.

I'll describe what I think to be the "right way" to do the same with our ManagedBean.

We have to intercept beforePhase event. I chose to do that at a f:view component (maybe you can do it at another point, but this is a good one).

But we need to intercept 2 specific phases. JSF has 6 main phases, but the ones we need to be aware are (before) PhaseId.RENDER_RESPONSE and PhaseId.INVOKE_APPLICATION.


Because the JSF lifecycle doesn't iterate over all 6 phases when you request the page for the first time. It calls just one phase.

When you are requesting your page for the first time (not a postback), you will only get the RENDER_RESPONSE phase. And when you are requesting the page in a postback, you will get all 6 phases.

So when we are NOT in a postback, we need to intercept RENDER_RESPONSE, and when we ARE in a postback, we need to intercept INVOKE_APPLICATION.

Ok, ok. Shut up and show me the magic.

At your XHTML, you will need to place a beforePhase handler:

At your ManagedBean (in this case myBean) put these two methods:

I hope be helpfull.
I agree. Here's the link: http://aspose.com/file-tools
subject: On Load of every JSF Page
It's not a secret anymore!