permaculture playing cards
The moose likes JSF and the fly likes how to make jsf function work when page loads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "how to make jsf function work when page loads " Watch "how to make jsf function work when page loads " New topic

how to make jsf function work when page loads

tony altaico

Joined: Jan 31, 2009
Posts: 23

I have a calculate method in java which needs to be run when the jsf page loads. What I need to use in my jsf page to make this function run once when the jsf page loads at first ?

In order to fix it I have written by function inside <hutputText> but that's not the clear or absolute solution. Any assistance will be appreciated.


Here is what I did to resolve it but that should not be the exact solution. Calculate methods works inside getMessage() function.

Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 200

Your best bet may be to write a Phase Listener.

If you're familiar with the JSF Life Cycle then you can create a Phase Listener that will run the method you need at the point in the life cycle that you need it.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16395

Rule #1 in JSF: Write JSF-specific code only when all else fails. JSF is supposed to be your friend, not your prison.

A lot depends on how this page is going to be accessed.

A common case for me is when I'm about to edit a database record. In that case, I am generally coming from some other JSF page, and the control that sends me to this new page will typically have an action method that invokes a "beginEdit()" method on the new page's backing bean to cause the record to be loaded and the edit bean is initialized. A side benefit of this is that if the load fails, the "from" page can present an error instead of dispatching a bad view.

In JSF2, you can also set up parameterized views if you want. Or use the PrettyFaces add-in. These features allow you to work with "bookmarkable" URLS so that you can jump directly into the target page without coming from another JSF page.

Another option is to use the @PostConstruct annotation to cause logic to be executed after managed properties have been injected. However, this behavior actually is done when the Model is created, not the View (page), so the ability to literally act on "page load" is dependent on scope and usage.

Likewise the method you indicated of attaching code to an outputText element. I frequently use a variant of this to initialize selection lists. The main problem here is that you have the same caveats as on @PostConstruct, plus ideally you should include mechanisms to avoid performance and/or side effects that can come when a single view request accesses the property more than once (a frequent occurrence in JSF). A variation of this technique I use in conjunction with things like "beginEdit()" above invalidates any previously-cached information so that a new page load would then update the data.

Finally, you should be able to "tickle" the Model using an AJAX onload() JavaScript method.

There are other techniques as well, but most of what I do manages with just a few of the above techniques. And, if you look at them in detail, pretty much none of them require any javax.faces class methods, hooks into the JSF framework or the FacesContext - they're POJO code. Hence Rule #1.

One thing that does require special attention in JSF is the postback mechanism. There are 3 kinds of page loads to keep in mind:

1. Page load due to initial request for page.
2. Page load in response to postback
3. Return to a page previously used (possibly with different data).

Customer surveys are for companies who didn't pay proper attention to begin with.
I agree. Here's the link:
subject: how to make jsf function work when page loads