09 November, 2012

Execute Code in Page Load in ADF


I want to execute a piece of code in page load, For implementing this request I can do it using two solutions.

1- PagePhaseListener Interface
PagePhaseListener allows to write global code which executes in every page at my application.
I will create a class implements oracle.adf.controller.v2.lifecycle.PagePhaseListener.PagePhaseListener Interface and overide afterPhase method and then add this class as Phase Listener in  /META-INF/adf-settings.xml

2- BeforePhase Property of View
I will bind this property to a method in backing bean which contains the code I want to execute.


Demo
I want to display welcome page in page load
I will alert user by "Welcome in my application"

1- PagePhaseListener Interface

a- Create a Class implements oracle.adf.controller.v2.lifecycle.PagePhaseListener.PagePhaseListener Interface

b- Override afterPhase method and write the code which you want for executing in every page load in your application

   @Override  
   public void afterPhase(PagePhaseEvent pagePhaseEvent) {  
     if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_RENDER_ID) {  
       runJavaScriptCode("alert(\"Welcome in my application\");");  
     }  
   }  

You can get code of runJavaScriptCode from Execute Javascript code from Java Code.

c- Register customPagePhaseListener class in  /META-INF/adf-settings.xml
you can put the below text in XML file

 <?xml version="1.0" encoding="windows-1252" ?>  
 <adf-settings xmlns="http://xmlns.oracle.com/adf/settings">  
   <adf-controller-config xmlns="http://xmlns.oracle.com/adf/controller/config">  
     <lifecycle>  
       <phase-listener>  
         <listener-id>customListener</listener-id>  
         <class>demo.mah.view.customPagePhaseListener</class>  
       </phase-listener>  
     </lifecycle>  
   </adf-controller-config>  
 </adf-settings>  
   

2- BeforePhase Property of View
I will change  BeforePhase Property of View to onBeforePhase method in backing bean



I will add the below code to onBeforePhase method

   public void onBeforePhase(PhaseEvent phaseEvent) {  
     // Add event code here...  
     runJavaScriptCode("alert(\"Welcome in my application\");");  
   }  

Thanks

No comments:

Post a Comment

ADF : Scope Variables

Oracle ADF uses many variables and each variable has a scope. There are five scopes in ADF (Application, Request, Session, View and PageFl...