XRPL - is*Phase() API

コード 1 - testPhaseExecution.xsp:
 
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
  <xp:this.afterRestoreView>
    <![CDATA[#{javascript:println("RESTORE_VIEW: " + debugBean.isRestoreViewPhase());}]]>
  </xp:this.afterRestoreView>
  <xp:button value="Test Phases" id="button1">
    <xp:this.rendered>
      <![CDATA[#{javascript:
        println("RESTORE_VIEW: " + debugBean.isRestoreViewPhase());
        println("APPLY_REQUEST_VALUES: " + debugBean.isApplyRequestValuesPhase());
        println("PROCESS_VALIDATIONS: " + debugBean.isProcessValidationsPhase());
        println("UPDATE_MODEL_VALUES: " + debugBean.isUpdateModelValuesPhase());
        println("INVOKE_APPLICATION: " + debugBean.isInvokeApplicationPhase());
        println("RENDER_RESPONSE: " + debugBean.isRenderResponsePhase());
        // note same as: println("RENDER_RESPONSE: " + view.isRenderingPhase());
        return true;
    }]]>
    </xp:this.rendered>
    <xp:eventHandler event="onclick" submit="true"
      refreshMode="complete">
      <xp:this.action>
        <![CDATA[#{javascript:
          println("INVOKE_APPLICATION: " + debugBean.isInvokeApplicationPhase());
        }]]>
      </xp:this.action>
    </xp:eventHandler>
  </xp:button>
</xp:view>
 
-----------------------------------------------------------------------------------------------------------------
 
コード 2 - DebugBean.java:
 
package com.ibm.xsp.masterclass.beans;
 
import java.io.Serializable;
 
import javax.faces.context.FacesContext;
 
import com.ibm.xsp.masterclass.lifecycle.DebugBeanPhaseListener;
 
/*
 * @author Tony McGuckin, IBM
 */
public class DebugBean implements Serializable {
 
    private static final long serialVersionUID = -1698889483899280998L;
 
    public static final String BEAN_NAME = "debugBean";
 
    public static DebugBean instance;
 
    protected boolean debug = false;
 
    // ------------------------------------------------------------------------
 
    public DebugBean() {
  instance = this;
    }
 
    // ------------------------------------------------------------------------
 
    public static DebugBean getInstance() {
  if (null == instance) {
      FacesContext context = FacesContext.getCurrentInstance();
      instance = (DebugBean) context.getApplication().getVariableResolver().resolveVariable(context, BEAN_NAME);
  }
  return instance;
    }
 
    // ------------------------------------------------------------------------
 
    public boolean isDebug() {
  return debug;
    }
 
    public void setDebug(boolean debug) {
  this.debug = debug;
    }
 
    public void setDebug(String debug) {
  this.debug = Boolean.parseBoolean(debug);
    }
 
    // ------------------------------------------------------------------------
     
    public boolean isRestoreViewPhase(){
  DebugBeanPhaseListener listener = DebugBeanPhaseListener.getInstance();
  return (null != listener && listener.isRestoreViewPhase());
    }
     
    public boolean isApplyRequestValuesPhase(){
  DebugBeanPhaseListener listener = DebugBeanPhaseListener.getInstance();
  return (null != listener && listener.isApplyRequestValuesPhase());
    }
     
    public boolean isProcessValidationsPhase(){
  DebugBeanPhaseListener listener = DebugBeanPhaseListener.getInstance();
  return (null != listener && listener.isProcessValidationsPhase());
    }
     
    public boolean isUpdateModelValuesPhase(){
  DebugBeanPhaseListener listener = DebugBeanPhaseListener.getInstance();
  return (null != listener && listener.isUpdateModelValuesPhase());
    }
     
    public boolean isInvokeApplicationPhase(){
  DebugBeanPhaseListener listener = DebugBeanPhaseListener.getInstance();
  return (null != listener && listener.isInvokeApplicationPhase());
    }
     
    public boolean isRenderResponsePhase(){
  DebugBeanPhaseListener listener = DebugBeanPhaseListener.getInstance();
  return (null != listener && listener.isRenderResponsePhase());
    }
 
    // ------------------------------------------------------------------------
 
} // end DebugBean
 
-----------------------------------------------------------------------------------------------------------------
 
コード 3 - DebugBeanPhaseListener.java:
 
package com.ibm.xsp.masterclass.lifecycle;
 
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
 
import com.ibm.xsp.masterclass.beans.DebugBean;
 
/*
 * @author Tony McGuckin, IBM
 */
public class DebugBeanPhaseListener implements javax.faces.event.PhaseListener {
 
    private static final long serialVersionUID = 1L;
     
    private static DebugBeanPhaseListener instance;
     
    private PhaseId phaseId;
     
    public DebugBeanPhaseListener(){
  instance = this;
    }
 
    // ------------------------------------------------------------------------
     
    public static DebugBeanPhaseListener getInstance(){
  if(null == instance){
      instance = new DebugBeanPhaseListener();
  }
  return instance;
    }
 
    // ------------------------------------------------------------------------
     
    public boolean isRestoreViewPhase(){
  return (null != phaseId && phaseId.equals(PhaseId.RESTORE_VIEW));
    }
     
    public boolean isApplyRequestValuesPhase(){
  return (null != phaseId && phaseId.equals(PhaseId.APPLY_REQUEST_VALUES));
    }
     
    public boolean isProcessValidationsPhase(){
  return (null != phaseId && phaseId.equals(PhaseId.PROCESS_VALIDATIONS));
    }
     
    public boolean isUpdateModelValuesPhase(){
  return (null != phaseId && phaseId.equals(PhaseId.UPDATE_MODEL_VALUES));
    }
     
    public boolean isInvokeApplicationPhase(){
  return (null != phaseId && phaseId.equals(PhaseId.INVOKE_APPLICATION));
    }
     
    public boolean isRenderResponsePhase(){
  return (null != phaseId && phaseId.equals(PhaseId.RENDER_RESPONSE));
    }
 
    public PhaseId getPhaseId() {
  return PhaseId.ANY_PHASE;
    }
 
    // ------------------------------------------------------------------------
 
    public void beforePhase(PhaseEvent event) {
  try {
      phaseId = event.getPhaseId();
      if (DebugBean.getInstance().isDebug()) {
    if (event.getPhaseId().equals(PhaseId.RESTORE_VIEW)) {
        System.out.println(" ");
        System.out.println("Request:\tStarted...");
    }
    System.out.println(" ");
    System.out.println("Lifecycle:\tBefore Phase: " + event.getPhaseId());
      }
  } catch (Exception e) {
      e.printStackTrace();
  }
    }
 
    // ------------------------------------------------------------------------
 
    public void afterPhase(PhaseEvent event) {
  try {
      phaseId = event.getPhaseId();
      if (DebugBean.getInstance().isDebug()) {
    System.out.println("Lifecycle:\tAfter Phase: " + event.getPhaseId());
    System.out.println(" ");
    if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
        System.out.println("Request:\tCompleted.");
        System.out.println(" ");
    }
      }
  } catch (Exception e) {
      e.printStackTrace();
  }
    }
} // end DebugBeanPhaseListener
 
-----------------------------------------------------------------------------------------------------------------
 
コード 4 - faces-config.xml:
 
<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
  <lifecycle>
    <phase-listener>com.ibm.xsp.masterclass.lifecycle.DebugBeanPhaseListener</phase-listener>
  </lifecycle>
  <managed-bean>
    <managed-bean-name>debugBean</managed-bean-name>
    <managed-bean-class>com.ibm.xsp.masterclass.beans.DebugBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
      <property-name>debug</property-name>
      <value>false</value>
    </managed-property>
  </managed-bean>
</faces-config>





XPages リクエストプロセスライフサイクルの実行完了を検証するカスタムアプリケーションコードでフェーズの実行ポイントをチェックする Managed Bean で使用します:

debugBean.isRestoreViewPhase()
debugBean.isApplyRequestValuesPhase()
debugBean.isProcessValidationsPhase()
debugBean.isUpdateModelValuesPhase()
debugBean.isInvokeApplicationPhase()
debugBean.isRenderResponsePhase()

「Test Phases」ボタンをクリックすると次のようなサーバーコンソールの出力を生成します:


Request:       Started...

Lifecycle:     Before Phase: RESTORE_VIEW 1
RESTORE_VIEW: true
Lifecycle:     After Phase: RESTORE_VIEW 1


Lifecycle:     Before Phase: APPLY_REQUEST_VALUES 2
RESTORE_VIEW: false
APPLY_REQUEST_VALUES: true
PROCESS_VALIDATIONS: false
UPDATE_MODEL_VALUES: false
INVOKE_APPLICATION: false
RENDER_RESPONSE: false
Lifecycle:     After Phase: APPLY_REQUEST_VALUES 2


Lifecycle:     Before Phase: PROCESS_VALIDATIONS 3
RESTORE_VIEW: false
APPLY_REQUEST_VALUES: false
PROCESS_VALIDATIONS: true
UPDATE_MODEL_VALUES: false
INVOKE_APPLICATION: false
RENDER_RESPONSE: false
Lifecycle:     After Phase: PROCESS_VALIDATIONS 3


Lifecycle:     Before Phase: UPDATE_MODEL_VALUES 4
RESTORE_VIEW: false
APPLY_REQUEST_VALUES: false
PROCESS_VALIDATIONS: false
UPDATE_MODEL_VALUES: true
INVOKE_APPLICATION: false
RENDER_RESPONSE: false
Lifecycle:     After Phase: UPDATE_MODEL_VALUES 4


Lifecycle:     Before Phase: INVOKE_APPLICATION 5
INVOKE_APPLICATION: true
Lifecycle:     After Phase: INVOKE_APPLICATION 5


Lifecycle:     Before Phase: RENDER_RESPONSE 6
RESTORE_VIEW: false
APPLY_REQUEST_VALUES: false
PROCESS_VALIDATIONS: false
UPDATE_MODEL_VALUES: false
INVOKE_APPLICATION: false
RENDER_RESPONSE: true
Lifecycle:     After Phase: RENDER_RESPONSE 6

Request:       Completed.

XPages
katoman
August 18, 2015 10:47 AM
Rating
0





No comments yetLogin first to comment...