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