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.

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

   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">  

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\");");  


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...