×
☰ See All Chapters

JSF Phase Event Listener

  • Phase event get fired during JSF application request processing life cycle. Phase events are not fired by any UI components. 

  • JSF request processing life cycle involves six phases and events are fired at start and end of each phase. 

  • To attach listener to phase events, you have to implement the PhaseListener interface. You should implement the methods beforePhase() and afterPhase(). 

  • beforePhase() method will be invoked before phase begins and afterPhase() will be invoked after phase gets completed. 

  • You should register the phase listener class in faces-config.xml as below: 

        <lifecycle>

                <phase-listener>com.java4coding.MyListener</phase-listener>

        </lifecycle>

JSF Phase Event Listener Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>JSF_PhaseEventListener</artifactId>

        <packaging>war</packaging>

        <version>1.0-SNAPSHOT</version>

        <dependencies>

                <dependency>

                        <groupId>com.sun.faces</groupId>

                        <artifactId>jsf-api</artifactId>

                        <version>2.2.6</version>

                </dependency>

                <dependency>

                        <groupId>com.sun.faces</groupId>

                        <artifactId>jsf-impl</artifactId>

                        <version>2.2.6</version>

                </dependency>

        </dependencies>

        <build>

                <finalName>JSF_PhaseEventListener</finalName>

                <plugins>

                        <plugin>

                                <groupId>org.apache.maven.plugins</groupId>

                                <artifactId>maven-compiler-plugin</artifactId>

                                <version>2.3.1</version>

                                <configuration>

                                        <source>1.9</source>

                                        <target>1.9</target>

                                </configuration>

                        </plugin>

                </plugins>

        </build>

</project>

web.xml

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "https://java.sun.com/dtd/web-app_2_3.dtd" >

 

<web-app>

        <display-name>Archetype Created Web Application</display-name>

        <servlet>

                <servlet-name>Faces Servlet</servlet-name>

                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

                <load-on-startup>1</load-on-startup>

        </servlet>

        <servlet-mapping>

                <servlet-name>Faces Servlet</servlet-name>

                <url-pattern>*.xhtml</url-pattern>

        </servlet-mapping>

</web-app>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config xmlns="https://xmlns.jcp.org/xml/ns/javaee"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"

        version="2.2">

 

        <lifecycle>

                <phase-listener>com.java4coding.MyListener</phase-listener>

        </lifecycle>

 

</faces-config>

MyListener.java

package com.java4coding;

 

import javax.faces.event.PhaseEvent;

import javax.faces.event.PhaseId;

import javax.faces.event.PhaseListener;

 

public class MyListener implements PhaseListener {

 

        @Override

        public void afterPhase(PhaseEvent phaseEvent) {

                System.out.println("AFTER PHASE ID:- "        + phaseEvent.getPhaseId().toString());

        }

 

        @Override

        public void beforePhase(PhaseEvent phaseEvent) {

                System.out.println("BEFORE PHASE ID:- "        + phaseEvent.getPhaseId().toString());

        }

 

        @Override

        public PhaseId getPhaseId() {

                return PhaseId.ANY_PHASE;

        }

 

}

HelloBean.java

package com.java4coding;

 

import javax.faces.bean.ManagedBean;

 

@ManagedBean

public class HelloBean {

 

        private String name;

        private String password;

 

        public String getName() {

                return name;

        }

 

        public void setName(String name) {

                this.name = name;

        }

 

        public String getPassword() {

                return password;

        }

 

        public void setPassword(String password) {

                this.password = password;

        }

 

        public String checkUser() {

                String str = "failure";

                if ((name.equalsIgnoreCase("Manu_M") && (password.equalsIgnoreCase("mm")))) {

                        str = "success";

                        return str;

                } else {

                        return str;

 

                }

        }

}

login.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="https://www.w3.org/1999/xhtml"

        xmlns:h="https://xmlns.jcp.org/jsf/html"

        xmlns:f="https://xmlns.jcp.org/jsf/core">

 

<h:head></h:head>

<h:body style="background-color:yellow">

        <h:form>

                <h1>Please enter your details</h1>

                <h:panelGrid columns="2">

                        <h:outputText value="Enter your name" />

                        <h:inputText id="nme" value="#{helloBean.name}" />

 

                        <h:outputText value="Enter your password" />

                        <h:inputSecret id="pwd" value="#{helloBean.password}" />

 

                        <h:commandButton value="LOGIN" action="#{helloBean.checkUser}" />

                </h:panelGrid>

        </h:form>

</h:body>

</html>

failure.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="https://www.w3.org/1999/xhtml"

        xmlns:h="https://xmlns.jcp.org/jsf/html"

        xmlns:f="https://xmlns.jcp.org/jsf/core">

       

        <h:head></h:head>

        <h:body style="background-color:red">

        <h1>Hello Mr. <h:outputText value="#{helloBean.name}" /> You are not welcome <br/>

        You are invalid user

        </h1>

        </h:body>

</html>

success.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="https://www.w3.org/1999/xhtml"

        xmlns:h="https://xmlns.jcp.org/jsf/html"

        xmlns:f="https://xmlns.jcp.org/jsf/core">

       

        <h:head></h:head>

        <h:body style="background-color:green">

        <h1>Hello Mr. <h:outputText value="#{helloBean.name}" /> You are welcome <br/>

        Thank You for logging in...

        </h1>

        </h:body>

</html>

Eclipse Project Directory Structure

jsf-phase-event-listener-0

Server console output

jsf-phase-event-listener-1


All Chapters
Author