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…… 🙂

Vinay

I am an Oracle ACE in Oracle ADF/Webcenter. Sr Java Consultant-working on Java/J2EE/Oracle ADF/Webcenter Portal/ content and Hibernate for several years. I'm an active member of the OTN JDeveloper/Webcenter forum. Passionate about learning new technologies. I am here to share my knowledge. Give your views and suggestion on [email protected] .

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusYouTube

  • Ramandeep Nanda

    Nope not for internal use, do you also mind posting the reference ?

  • @ramandeep – i will try to find it out.. if not i will delete my note.. and after all i can refer to your blog link for sure.

    I got alot help from your blog http://ramannanda.blogspot.ca/2011/09/adf-printable-page-behaviour-in-page.html

  • Shanto

    Nice post vinay.. Thanx

  • Amitt Ranjan

    I was trying this code in my Application. My jsff is a part of bounded taskflow called as region. i wrote the sop’s to detect by found that control flow never went to the method:

    public boolean refreshRegion(RegionContext regionContext){


    }

    please advise

    • vinay kumar

      it shld went. Can you provide steps how you implemented ?

      • Amitt Ranjan

        I did made the backing bean implementing the RegionController:

        public class AccSummary implements RegionController {

        ..

        }

        Made a method call on RENDER_MODEL:

        public boolean refreshRegion(RegionContext regionCtx)

        {

        System.out.println(“Inside refreshRegion refreshFlag “+regionCtx.getRefreshFlag()+” RegionBinding.RENDER_MODEL “+RegionBinding.RENDER_MODEL);

        int refreshFlag = regionCtx.getRefreshFlag();

        if (refreshFlag == RegionBinding.RENDER_MODEL)

        {

        ClickMe_Click();

        }

        }

        saw the page getting loaded 1st time and when ClickMe_Click() is called after the page is gets loaded, it give a null pointer as it is not able to recognize the component which is on the page. Null pointer occurs in following line of ClickMe_Click():

        this.getPrevBalanceText().setValue(“”);

        where prevBalanceText is a RichOutputText on the jsff page which has already been defined initially.

        please advise what to do?

        • vinay kumar

          it will not recongise the component. Either you handle null pointer exception.. check if it is null don’t call getter of component. use if and else.

          • Amitt Ranjan

            Thanks for your response vinay.Can you Please advise how to set the components after the page gets rendered in this case

          • vinay kumar

            Amit can u share your exact requirement?May be i can help u

          • Amitt Ranjan
          • vinay kumar

            use javascript to set value in component using client listener or if it is first page of task flow u can also use default activity of task flow as well

          • Amitt Ranjan

            vinay, i am not sure how to reach you, but can you look into one more different issue @ https://community.oracle.com/thread/2620722

          • vinay kumar

            i replied on forum

    • vinay kumar

      and if it is part of task flow then better you write a method call in task flow before the jsff view.

  • max

    Hi Vinay,

    when i deploy my application with jsff and RegionController on webLogic12c i get java.lang.NullPointerException

    in line “if(currentPhase.getOrdinal()==PhaseId.RENDER_RESPONSE.getOrdinal())”

    requestMap does not have “oracle.adfinternal.view.faces.lifecycle.CURRENT_PHASE_ID”

    My requestMap is:

    Key : _current_datacontrol_frame_ Value : [email protected]

    Key : _adfc.bindingContext Value : [email protected]

    Key : oracle.adfinternal.view.faces.lifecycle.ORIGINAL_CREATE_VIEW_ID Value : /main.jsf

    Key : oracle.adf.controller.pageFlowCache Value : [email protected]

    Key : org.apache.myfaces.trinidadinternal.webapp.AdfacesFilterImpl.EXECUTED Value : true

    Key : bindings Value : view_pageDefs_RadnalPageDef_WEB_INF_TestRadnal_xml_TestRadnal

    Key : oracle.adf.controller.backingBeanScope Value : oracle.adfinterna[email protected]1dfc44a4

    Key : org.apache.myfaces.trinidadinternal.context.AdfFacesPhaseListener.POSTBACK Value : false

    Key : org.apache.myfaces.trinidadinternal.InitialViewRoot Value : [email protected]

    Key : oracle.adfinternal.controller.lifecycle.MetadataCommitHandler Value : [email protected]37e8a

    Key : org.apache.myfaces.trinidad.util.RequestStateMap Value : {oracle.adfinternal.view.faces.context.PageManager._[email protected]10d899a7, org.apache.myfaces.trinidadinternal.config.dispatch.DispatchResponseConfiguratorImpl.APPLIED=true, oracle.adfinternal.view.faces.config.rich.RegistrationConfigurator.IN_REQUEST=true, org.apache.myfaces.trinidadinternal.config.GlobalConfiguratorImpl.REQUEST_CONTEXT[email protected]7c4fab94, org.apache.myfaces.trinidadinternal.config.GlobalConfiguratorImpl.REQUEST_TYPE=SERVLET, org.apache.myfaces.trinidadinternal.config.GlobalConfiguratorImpl.IN_REQUEST=false}

    Key : oracle.adf.controller.requestState Value : [email protected]

    Key : ADFCurrentViewRoot Value : [email protected]

    Key : oracle.adf.controller.moKeyCache Value : [email protected]800f838

    Key : ADFLifecycleContext Value : [email protected]f21237

    Key : oracle.adf.model.BindingContext.requestContext Value : {}

    Key : [email protected] Value : [email protected]

    Key : oracle.adf.controller.regionRequestData Value : {70brkmxg0_4=oracle.adf.co[email protected]31c50480}

    Key : oracle.dms.context.http.WebRequestContextProcessed Value : true

    Key : oracle.adfinternal.view.faces.webapp.rich.RichWindowManager#WINDOW_ID Value : w0

    Key : oracle.adf.controller.activityBreakpointState Value : {}

    Key : __request_refcount__ Value : 1

    Key : oracle.adf.controller.metadataCache Value : oracle.adfinternal.controller[email protected]7daa8906

    Key : oracle.adf.controller.regionStack Value : [oracle[email protected]470a295e]

    Key : oracle.adf.controller.AdfcContext Value : [email protected]

    Key : _afrWindowMode Value : 0

    Can you help me?

    • vinay kumar

      what your environment-Which version of jdev you using? Are you able to run n weblogic 11g.

  • Susanto Paul

    Dear Vinay,

    How can I call a method on JSFF unload?

    Thanks & Regards,
    Susanto

    • vinay kumar

      hi susanto,

      whts te usecase?wt u want to achieve