How to execute method on page load method call in ADF Faces

Posted by

1. Open the page definition of your JSF Page – TestOnPageLoad.jspx

2. Add the controllerlclass in the <PageDefinition> tag as given below

<pageDefinition xmlns=”http://xmlns.oracle.com/adfm/uimodel”
version=”11.1.1.60.13″ id=”TestOnPageLoadPageDef”
Package=”view.pageDefs”
ControllerClass=”com.test.bean.PageLoadBackingBean”>
3. Create the class – executeOnPageLoadMethod which implements PagePhaseListener and override the beforePhase method.

import oracle.adf.controller.faces.context.FacesPageLifecycleContext;
import oracle.adf.controller.v2.lifecycle.Lifecycle;
import oracle.adf.controller.v2.lifecycle.PagePhaseEvent;
import oracle.adf.controller.v2.lifecycle.PagePhaseListener;

import oracle.binding.BindingContainer;

public class executeOnPageLoadMethod implements PagePhaseListener {
private BindingContainer bc = null;

public void beforePhase(PagePhaseEvent event) {
FacesPageLifecycleContext ctx =
(FacesPageLifecycleContext)event.getLifecycleContext();
if (event.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) {
bc = ctx.getBindingContainer();
onPageLoad();
bc = null;
}
}

public void afterPhase(PagePhaseEvent event) {
}

public void onPageLoad() {

}

}

3. Open the Class PageLoadBackingBean and extend the class – executeOnPageLoadMethod
public class PageLoadBackingBean extends executeOnPageLoadMethod {

4. Implement the method onPageLoad in the class – PageLoadBackingBean which is defined in the class – executeOnPageLoadMethod

/**
* Method to override the onPageLoad method of the executeOnPageLoadMethod
* which implements the PagePhaseListener Class
*/
public void onPageLoad() {
if (!AdfFacesContext.getCurrentInstance().isPostback()) {
executeSomeMethod();
}
}

5. public void executeSomeMethod() {

// execute some logic here
}