Jsf application events example

JSF provides system event listeners to handle application events which fired when application start or stop or during JSF lifecycle.

JSF application events:

Application Event Description
  1. PostConstructApplicationEvent
JSF fires PostConstructApplicationEvent when application starts. It can be used to perform initialization tasks after application has started.
  1. PreDestroyApplicationEvent
JSF fires PreDestroyApplicationEvent when application is about to shut down.It can be used to perform a cleanup tasks before application is about to be shut down.
  1. PreRenderViewEvent
JSF fires PreRenderViewEvent before a JSF page is to be displayed. It can be used to authenticate user and provide restricted access to JSF View.

An application event can be handled by following ways:

1. Method Binding.
2. SystemEventListener Interface.

Method Binding:

Use the managed bean method in listener attribute of f:event.

Bean class:

@ManagedBean(name="testBean")
@SessionScoped
public class TestBean{ 
	public void handleEvent(ComponentSystemEvent event){
		//method body
	} 
}

JSF component:

<f:event listener="#{testBean.handleEvent}" type="preRenderView" />

SystemEventListener Interface:

Creates a class which implements SystemEventListener interface and override the processEvent() method. Put its entry into faces-config.xml file.

Java class:

public class SystemEventListenerTestClass implements SystemEventListener {
	@Override
	public void processEvent(SystemEvent event)
			throws AbortProcessingException { 
		//method body 
	}
}

Faces-config.xml:

<system-event-listener>
   <system-event-listener-class>
      com.w3schools.business.SystemEventListenerTestClass
   </system-event-listener-class>
   <system-event-class>
      javax.faces.event.PostConstructApplicationEvent
   </system-event-class>    					
</system-event-listener>

Example:

test.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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">
	<h:head>
		<title>JSF application event example.</title>
	</h:head>
    <h:body>
    	<h2>JSF application event example.</h2>
    </h:body>
 
</html>

SystemEventTest.java

import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.PreDestroyApplicationEvent;
 
public class SystemEventTest implements SystemEventListener {
	@Override
	public void processEvent(SystemEvent event) 
	                         throws AbortProcessingException {
	    if(event instanceof PostConstructApplicationEvent){
	      System.out.println("" +
	      		"PostConstructApplicationEvent is Called.");
	    }
 
	    if(event instanceof PreDestroyApplicationEvent){
	      System.out.println("" +
	      		"PreDestroyApplicationEvent is Called.");
	    }	    
	  }
 
	  @Override
	  public boolean isListenerForSource(Object source) {
	    return (source instanceof Application);	    
	  }
}

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 
<application>
 <system-event-listener>
   <system-event-listener-class>
      com.w3schools.business.SystemEventTest
   </system-event-listener-class>
   <system-event-class>
      javax.faces.event.PostConstructApplicationEvent
   </system-event-class>              
 </system-event-listener>
</application>
 
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>
         javax.faces.webapp.FacesServlet
	<servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
 
</web-app>

URL:

http://localhost:7001/JSFExample40/faces/test.xhtml

Output:

JSF example40-1
 
Download this example.