JSF onload action during restore and render response phase
November 10, 2008 14 Comments
When comparing JSF with action based frameworks like Struts, Webwork, etc. one of the major missing feature is onload action.
I tried googling to find out various options to do onload action. Many frameworks and external components does in different ways, most of them requires additional XML files which I hate to maintain too many XML files.
Shale uses faces-config.xml to map view ids and managed beans which implement a ViewController interface. Seam allows to map view ids to method expressions via a pages.xml configuration file. JSF-COMP components adds additional onload-config.xml to map view id and onload action. All these solutions requires additional XML configuration file. But I was looking for a simple solution without introducing additional XML file. In some forums mentioned that <hx:scriptCollector> can be used to execute onload action I like this approach but it is not an open source component.
JSF1.2 introduced beforePhase and afterPhase attribute for the component <f:view>, for some reason this solution didn’t work for me with facelet integration.
Finally after a long search I wrote a custom component very similar to <hx:scriptCollector> to invoke backing bean methods on restore and render response phases. Invoking method on render response phase helped me a lot when compared to lazy initialization in getter methods. For an instance I had a page which displays user information retrieved from database while retrieving values from database there might be some warnings/errors which needs to be displayed on top of the page and then remaining part of the page. Which is not possible in lazy initialization because message component gets executed before the datatable component. With my onload component placing in beginning of the page to do all the initialization stuffs and add any error/warning messages into FacesContent so that messages component can render the error message at top of the page. Everything worked fine.
Hope all of you will get benefit of this component. The archive file contains the component along with faces-config.xml file which needs to be merged with your project faces-config.xml file.
Sample code at GitHub: https://github.com/venkatsalem/jsf-onload
Hi Venkat,
nice to read this article about JSF onload action. Am also trying to go for alternative to onload action, for some reason my PM is not accepting it. Am not able to download the above LucidComponent.jar. Can you please share it with me by sending across to my mail srinadhv9@gmail.com.
Thanks in Advance
FYI, the problem with Facelets is that one of the properties on is named incorrectly. See https://facelets.dev.java.net/issues/show_bug.cgi?id=294. This hasn’t been fixed, but it works fine if you use “beforePhaseListener” and “afterPhaseListener”.
Hello:
I am trying to download the LucidComponent, however it comes downloaded as an XML file instead of a RAR. I would appreciate any assistance you may offer.
Added jar version of download, hope this will helps to solve the download issues.
None of the free web hosting allows zip extension hence hosted in jar and rar format ;).
Great!! superb way of teaching us manipulating JSF needs
cool. will it call the method every time the page is loaded?
Yes, it will invoke for every time page loads.
Hi Venkat,
Does this work only for JSF1.2 spec. Iam working with Apache MyFaces Trinidad 1.0.10. I get compilation errors(Invokemethod.java) in the following places context.getELContext() getValueExpression. Also it shows error in javax.faces.webapp.UIComponentELTag
Yes, the code work only in JSF1.2. To make it working under JSF1.1 you have to convert all the MethodExpression to MethodBinding and ValueExpression to ValueBinding.
Hi, I just tried you’re component and I have a little problem with it. It keeps looking for a property while it should look for a method. I want to execute the getInit() method that I have in my backing bean and I always get an exception telling me that the property “getInit” cannot be found…!! Help me please..!
“getInit()” cannot be found. Make sure getInit() returns a String.
Hi Venkat
I liked the approach used for writing this component. And tried to use this jar file by adding the corresponding entry into faces-config file. When i was running, it was searching for the property getmethod and had throwd propertynotfoundexpection. Could you please help me with any snippet of code.
sorry, I don’t use JSF nowadays.Perhaps JSF2.0 should have better alternatives.
Pingback: JSF onload action « Coding fun