Calling Java method on page Load:-
·
Sometimes we have
requirements like calling some java method on loading of a page. To achieve it,
follow the below steps.
·
Drag and drop
af:serverListener from component palette on the page(on af:document) from where
java method is to be called. Suppose my backing bean name is Home and the
method to be called on page load is "onPageLoad".
So the code will something like
<af:serverListener
type="onLoadEvent" method="#{Home.onPageLoad}"/>
·
Now, drag and drop
af:resource from component palette on the page(on af:document). Select resource
type as javascript. Write the following code inside af:resource tag.
function triggerOnLoad(event)
{
AdfCustomEvent.queue(event.getSource(),"onLoadEvent",{},false);
return true;
}
·
Now our aim is to call
the function on page load. So drag and drop af:clientListner on page as we did
for the af:serverListener and af:resource. In method write the javascript
function i.e. triggerOnLoad and type should be load, as we are going to call the
method on page load.
<af:clientListener
type="load" method="triggerOnLoad"/>
That’s all. Run your page and it will call the
java method onPageLoad available inside the bean Home.
You
can download the sample workspace here