In this tutorial you will learn about the phaseListener tag in JSF 2.
JSF 2 phaseListener Example
In this tutorial you will learn about the phaseListener tag in JSF 2.
JSF <f:phaseListener> tag is used to add the PhaseListener to the component inside which it is nested. A PhaseListener is a listener of JSF lifecycle. It implements all the phases of JSF lifecycle.
Attributes of <f:phaseListener>
- type : This is not a required attribute. This attribute is used for specifying the full name of the PhaseListener implemented Java class.
- binding : This is not a required attribute. This attribute is used for binding the javax.faces.event.PhaseListener implemented object evaluator expression value.
Example :
Here an example is being given for you which will demonstrate about how to use JSF 2 f:phaseListener tag. To use the <f:phaseListener> tag you will be required to create a PhaseListener class i.e a class implements the javax.faces.event.PhaseListener. A class which implements this PhaseListener is called a PhaseListener class. This class must have override the methods befoePhase(), afterPhase(), and getPhaseId(). In these methods you can do your required task. Then you can use this tag into your JSF pages. So, At first I have created a class which implements the javax.faces.event.PhaseListener. And made this class a ManagedBean using the javax.faces.bean.ManagedBean and confined its scope for the request by using javax.faces.bean.RequestScoped. As well as this class has a data member named name and its setter getter methods to set and get its value so, this class can also be called a Java Bean class. Then created the JSF pages to take the input and to show the output. In the inputPage.jsf page I have used this tag inside the <h:commandButton>. Then changed the welcome-file-list in the auto generated web.xml file as <welcome-file>jsfPages/inputPage.xhtml</welcome-file>.
Directory Structure
PhaseListenerExample.java
package devmanuals; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseListener; import javax.faces.event.PhaseId; @ManagedBean(name="phaseBean") @RequestScoped public class PhaseListenerExample implements PhaseListener { FacesContext context = null; String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void afterPhase(PhaseEvent event) { System.out.println("Before :-"+event.getPhaseId()); } @Override public void beforePhase(PhaseEvent event) { System.out.println("Before - " + event.getPhaseId().toString()); } @Override public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } }
inputPage.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <head> <title>JSF phaseListener Tag</title> </head> <body> <h:form> Enter Text Here : <h:inputText value="#{phaseBean.name}" /> <h:commandButton action="outputPage" value="submit"> <f:phaseListener type="devmanuals.PhaseListenerExample"/> </h:commandButton> </h:form> </body> </html>
outputPage.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <head> <title>outputPage</title> </head> <body> <f:view> Hello, <h:outputText value="#{phaseBean.name}"/>! </f:view> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>jsfPhaseListener</display-name> <welcome-file-list> <welcome-file>jsfPages/inputPage.xhtml</welcome-file> </welcome-file-list> <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>/faces/*</url-pattern> <url-pattern>*.jsf</url-pattern> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> <param-value>resources.application</param-value> </context-param> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> </web-app>
How to Run
Firstly Start your web server then use this URL http://localhost:8080/jsfPhaseListener/jsfPages/inputPage.jsf into your web browser.
Output :
1. When you will execute this page first time then the output will be as follows
And on the console of eclipse for this example will be look as follows :
2. Now when you will give the value as follows :
3. And after giving the value to the textbox and clicking on the button named submit then the output will be as follows :
And the console of Eclipse for this example will be looked like as follows :
[ 0 ] Comments