Ejemplo Interceptor JSF

Febrero 1st, 2009
Vamos a crear un caso práctico, donde vamos a poder ver en que fases del ciclo de vida de las peticiones JSF, van interactuando nuestros métodos. 

Tenemos que crear un interceptor, que se va a encargar de capturar el inicio o el fin de cada fase. Para nuestro ejemplo, lo que vamos a hacer, es que antes de que se inicien o finalicen las fases, se mostrará por la consola, un mensaje de información.

Este interceptor, entre otras muchas utilidades, puede sernos útil a la hora de comprobar por qué no se están renderizando ciertos componentes o como afecta la propiedad immediate a nuestra ventana.

InterceptorFases.java

public class InterceptorFases extends SeamPhaseListener{

    private static final long serialVersionUID = 1L;

    public void beforePhase(PhaseEvent event){

        System.out.println(

            “————-ANTES DE LA FASE “+

            event.getPhaseId()+”———–”);

    }

    public void afterPhase(PhaseEvent event){

        System.out.println(

        “————-DESPUES DE LA FASE “+

        event.getPhaseId()+”———–”);

    }

   

}

A continuación debemos configurar el interceptor para que escuche los eventos lanzados por el PhaseEvent. Esto debemos definirlo en el fichero de configuración faces-config.xml. Tenemos que añadir un listener adicional, que será nuestro interceptor:

<lifecycle>

    <phase-listener>

        rutaDelPaquete.InterceptorFases

    </phase-listener>

</lifecycle>

Una vez hecho esto, si arrancamos nuestra aplicación, en cada fase del ciclo de vida entrará en acción el interceptor que hemos creado. Accediendo al objeto event, podréis acceder también al FacesContext, por tanto, aparte de esta utilidad, se le pueden dar otras muchas, dependiendo de lo que queráis en cada caso.

Interceptor example JSF

Febrero 1st, 2009

We are going to create a practical case, where we will see in that phases of the JSF lyfe cycle, are interacting our methods.

We must create an interceptor, that is going away to order to capture the beginning or the end of each phase. For our example, it is that before the phases begin or finalize, will be showned by the console (an information message).

This interceptor, among other many utilities, can be to us useful at the time of verifying so that certain components are not being render or as immediate property affects to our window. 

InterceptorFases.java

public class InterceptorFases extends SeamPhaseListener{

    private static final long serialVersionUID = 1L;

    public void beforePhase(PhaseEvent event){

        System.out.println(

            “————-BEFORE PHASE “+

            event.getPhaseId()+”———–”);

    }

    public void afterPhase(PhaseEvent event){

        System.out.println(

        “————-AFTER PHASE “+

        event.getPhaseId()+”———–”);

    }

   

}

 

Next we must configure the interceptor so that it listens to the events sent by the PhaseEvent. This we must define it in the configuration file faces-config.xml. We must add to listener additional, that will be our interceptor: 

<lifecycle>

    <phase-listener>

        package.InterceptorFases

    </phase-listener>

</lifecycle>

Once fact this, if we start our application, in each phase of the life cycle will be actioned the interceptor that we have created.

Acceding to the object event, you will be able to also accede to the FacesContext, therefore, aside from this utility, many can be given him other, depending on which you want in each case.