Call method on page load of JSFF (JSF fragment) in Oracle ADF

Requirement – Calling a method on page load at render response time for jsf fragment in oracle ADF

Solution – Alright. Few of ADF guys will say, use pagePhaseListener is answer. Well , you are not right .pagePhaseListener is work only in jspx , not in jsff. ๐Ÿ™ . As i already written about
pagePhaseListener in jspx here – invoke method on page load for jspx

Ok, we will use regionController,We will create a bean which implement regionController.

Create a bean which implements RegionController.

package com.techartifact.hte.bean.audittrailall;
 
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
 
import oracle.adf.model.RegionBinding;
import oracle.adf.model.RegionContext;
import oracle.adf.model.RegionController;
 
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

public class AuditTrailAllAppPhaseListener implements RegionController{
    public AuditTrailAllAppPhaseListener() {
        super();
    }
    
    public boolean refreshRegion(RegionContext regionContext)   // you need to override refresh region method.
    {
        int refreshFlag= regionContext.getRefreshFlag();
        FacesContext fctx = FacesContext.getCurrentInstance();
        //check internal request parameter
        Map requestMap = fctx.getExternalContext().getRequestMap();
        PhaseId currentPhase=(PhaseId)requestMap.get("oracle.adfinternal.view.faces.lifecycle.CURRENT_PHASE_ID");   
        if(currentPhase.getOrdinal()==PhaseId.RENDER_RESPONSE.getOrdinal())   // write custom logic of correct lifecycle phase.
        {
            Object showPrintableBehavior =
            requestMap.get("oracle.adfinternal.view.faces.el.PrintablePage");
            if (showPrintableBehavior != null)
            {
                if (Boolean.TRUE == showPrintableBehavior) 
                {
                ExtendedRenderKitService erks = null;
                erks =
                Service.getRenderKitService(fctx, ExtendedRenderKitService.class);
                //invoke JavaScript from the server
                erks.addScript(fctx, "window.print();");
                erks.addScript(fctx, "window.close();");
                    
                }
           }
                regionContext.getRegionBinding().refresh(refreshFlag);
        }
    return false;
    }
     
    public boolean validateRegion(RegionContext regionContext) 
    {
        regionContext.getRegionBinding().validate();
        return false;
    }
     
    public boolean isRegionViewable(RegionContext regionContext) 
    {
     return regionContext.getRegionBinding().isViewable();
    }
     
        public String getName() 
        {
        return null;
        }
 }

Ok , then you need to register this class file in jsff page definitoon file using controllerClass like below –

<?xml version="1.0" encoding="UTF-8" ?>
<pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel"
                version="11.1.1.61.92" id="audit_trail_allPageDef"
                ControllerClass="com.techartifact.hte.bean.audittrailall.AuditTrailAllAppPhaseListener"
                Package="com.techartifact.hte.pages.audittrailall">
  <parameters/>
  <executables>

That is all .Now run your page.you can write your java code on page code,in my case i call some javascript..

Happy coding with Vinay kumar in techartifact…… ๐Ÿ™‚

Invoke a method from Managed Bean when JSPX Page Loads in ADF.

Requirment -invoke a method from Managed Bean when JSPX Page Loads in ADF.

Suppose you have some method in managed bean, which you want to call on page load.For that
Your managed bean should implement implements PagePhaseListener i.e

public class vinayManagedBean implements PagePhaseListener{ }

You should import โ€œoracle.adf.controller.v2.lifecycle.PagePhaseListener;โ€
Now you will be calling method in beforePhase event of ADF page life cycle.In side the beforePhase ,pageLoad method will be called.You can write your custom method there.

public void beforePhase(PagePhaseEvent pagePhaseEvent) {
if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) {
onPageLoad();
}
}

public void afterPhase(PagePhaseEvent pagePhaseEvent) {
if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_RENDER_ID) {
// onPagePreRender();
}
}

public void onPageLoad() {
if (!AdfFacesContext.getCurrentInstance().isPostback()) {
// add your onPageLoad event here

// to set the View Criteria on the Iterator
doSomeOperation();  ///your custom method.
}
}

After all then right click on .jspx page and go to page definitions file .In your JSPX page definition make sure you add the ControllerClass that points to the managed bean that handles the onPageLoad event.

<pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel" version="11.1.2.60.17" id="InspectionTaskHomePageDef"
                Package="oper.view.pageDefs" ControllerClass="vinayManagedBean">
  <parameters/>

By doint this whenever you page loads, the onPageLoad() method gets invoked which inturn invokes the v() method. You can even call multiple methods in onPageLoad() method.

Note:- It will not work in jsff.