Search code examples
xpagesfaces-config

FacesContextListener implementation


In an attempt to implement the FacesContextListener, which seems to be the ideal place for the current challenge we are facing, I still struggle with its implementation. Attempts to declare it in the faces-config.xml or a construction similar to the ApplicationListener failed (as I probably reference things wrong (except for the class itself of course)).

Can someone provide directions / a short example regarding the implementation of the FacesContextListener?


Solution

  • Create a Java class which implements FacesContextListener interface.

    package ch.hasselba.xpages;
    
    import javax.faces.context.FacesContext;
    import com.ibm.xsp.event.FacesContextListener;
    
    public class MyFacesContextListener implements FacesContextListener {
    
        public void beforeContextReleased(FacesContext fc) {
            System.out.println("beforeContextReleased");
        }
    
        public void beforeRenderingPhase(FacesContext fc) {
            System.out.println("beforeRenderingPhase"); 
        }
    
    }
    

    Now, add an instance of the class to your XPage:

    importPackage( ch.hasselba.xpages )
    var fcl = new ch.hasselba.xpages.MyFacesContextListener();
    facesContext.addRequestListener( fcl );
    

    Hope this helps!

    EDIT: Here is a Java implementation with an anonymous Listener:

    package ch.hasselba.xpages;
    
    import javax.faces.context.FacesContext;
    import com.ibm.xsp.context.FacesContextExImpl;
    import com.ibm.xsp.event.FacesContextListener;
    
    public class MyObject {
    
        private transient FacesContextListener mFCListener;
    
        public MyObject() {
                mFCListener = new FacesContextListener() {
    
                public void beforeContextReleased(FacesContext fc) {
                    System.out.println("Before Releasing.");
                }
    
                public void beforeRenderingPhase(FacesContext fc) {
                    System.out.println("Before Rendering.");
                }
         };
    
         FacesContextExImpl fc = (FacesContextExImpl) FacesContext.getCurrentInstance();
         fc.addRequestListener( this.mFCListener );
     }
    }