Friday, April 5, 2013

Using Custom ADF Page Lifecycle to Invoke an onPageLoad Backing Bean Method


The SRDemo application contains a OnPageLoadBackingBeanBase class in the oracle.srdemo.view.util package that implements the PagePhaseListener interface described above using code like what's shown in Example 10-2. The class implements the interface's beforePhase() and afterPhase() methods so that in invokes an onPageLoad() method before the normal ADF prepare model phase, and an onPagePreRender() method after the normal ADF prepare render phase.


Example 10-2 PagePhaseListener to Invoke an onPageLoad() and onPagePreRender() Method
// In class oracle.srdemo.view.util.OnPageLoadBackingBeanBase
/**
 * Before the ADF page lifecycle's prepareModel phase, invoke a
 * custom onPageLoad() method. Subclasses override the onPageLoad()
 * to do something interesting during this event.
 * @param event
 */
public void beforePhase(PagePhaseEvent event) {
  FacesPageLifecycleContext ctx =
    (FacesPageLifecycleContext)event.getLifecycleContext();
  if (event.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) {
    bc = ctx.getBindingContainer();
    onPageLoad();
    bc = null;
  }
}
/**
 * After the ADF page lifecycle's prepareRender phase, invoke a
 * custom onPagePreRender() method. Subclasses override the onPagePreRender()
 * to do something interesting during this event.
 * @param event
 */    
public void afterPhase(PagePhaseEvent event) {
  FacesPageLifecycleContext ctx =
   (FacesPageLifecycleContext)event.getLifecycleContext();
  if (event.getPhaseId() == Lifecycle.PREPARE_RENDER_ID) {
    bc = ctx.getBindingContainer();
    onPagePreRender();
    bc = null;
  }
}
public void onPageLoad() {
  // Subclasses can override this.
}
public void onPagePreRender() {
  // Subclasses can override this.
}

If a managed bean extends the OnPageLoadBackingBeanBase class, then it can be used as an ADF page phase listener because it inherits the implementation of this interface from the base class. If that backing bean then overrides either or both of the onPageLoad() or onPagePreRender() method, that method will be invoked by the ADF Page Lifecycle at the appropriate time during the page request lifecycle.

The last step in getting such a backing bean to work, is to tell the ADF page definition to use the backing bean as its page controller for that page. As described above, this is done by setting the ControllerClass attribute on the page definition in question to an EL expression that evaluates to the backing bean.
The SRMain page in the SRDemo application uses the technique described in this section to illustrate writing programmatic code in the onPageLoad() method of the SRMain backing bean in the oracle.srdemo.view.backing package. Since that backing bean is named backing_SRMain in faces-config.xml, the ControllerClass property of the SRMain page's page definition is set to the EL expression "#{backing_SRMain}".

Source: http://docs.oracle.com/html/B25947_01/bcdcpal005.htm#sthref826
Related Posts Plugin for WordPress, Blogger...