Table of contents
1.
Introduction
2.
JSF Application Events
2.1.
1.PostConstructApplicationEvent
2.2.
2.PreDestroyApplicationEvent
2.3.
3.PreRenderViewEvent
3.
Methods used to handle application event:
3.1.
Method Binding:
3.1.1.
JSF Components
3.2.
SystemEventListener Interface
3.2.1.
Faces-config.xml
3.2.2.
Java Class
4.
Sample Program
4.1.
CodingNinjas.java
4.2.
CustomSystemEventListenerCodingNinjas.java
4.3.
home.xhtml
4.4.
faces-config.xhtml
4.5.
Output
5.
Frequently Asked Questions
5.1.
What is a JSF example?
5.2.
What is a JSF lifecycle?
5.3.
What is MVC in java?
5.4.
What is POJO?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

JSF Application Events

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you ever thought about when an application starts/stops or during the JSF lifecycle. It fires Application Events. So, How can the Application Events be handled? For this, JSF provides event listeners.

Now let's explore more about Application Events and different application events. 

 

JSF Application Events

Now let's have a look at the ways in which we can handle the application event.

1.PostConstructApplicationEvent

Occurs when the application launches. It can be used to carry out initialization duties upon application startup.

2.PreDestroyApplicationEvent

When an application is about to terminate, JSF fires a PreDestroyApplicationEvent. Before an application is shut down, it can be used to do cleanup chores

3.PreRenderViewEvent

Prior to displaying a JSF page, JSF fires a PreRenderViewEvent. It can be used to grant limited access to JSF View and authenticate users

 

Methods used to handle application event:

Method Binding:

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

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

JSF Components

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

SystemEventListener Interface

creates a class that replaces the processEvent() method and implements the SystemEventListener interface. Add a line for it to the faces-config.xml file.

Faces-config.xml

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

Java Class

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

Let's see an example for more clarification

Sample Program

CodingNinjas.java

package com.codingninjas.test;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ComponentSystemEvent;

@ManagedBean(name = "CodingNinjas", eager = true)
@SessionScoped
public class CodingNinjas implements Serializable {
   private static final long serialVersionUID = 1L;
   private String data = "sample data";

   public void handleEvent(ComponentSystemEvent event) {
      data = "Hello Coding Ninjas ";
   }

   public String getData() {
      return data;
   }
public void setData(String data) {
      this.data = data;
   }
}

CustomSystemEventListenerCodingNinjas.java

package com.CodingNinjas.test;

import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.PreDestroyApplicationEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class CustomSystemEventListenerCodingNinjas implements SystemEventListener {

   @Override
   public boolean isListenerForSource(Object value) {
      
      //only for Application
      return (value instanceof Application);
   }

   @Override
   public void processEvent(SystemEvent event) 
      throws AbortProcessingException {
      
      if(event instanceof PostConstructApplicationEvent) {
         System.out.println("Application Started. 
            PostConstructApplicationEvent occurred!");
      }
      
      if(event instanceof PreDestroyApplicationEvent) {
         System.out.println("PreDestroyApplicationEvent occurred.
            Application is stopping.");
      }
   }
}

home.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 Coding Ninjas</title>    
   </h:head>
   
   <h:body> 
      <h2>Application Events Examples</h2>
      <f:event listener = "#{codingNinjas.handleEvent}" type = "preRenderView" />
      #{codingNinjas.data}
   </h:body>
</html>

faces-config.xhtml

<?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.w3spoint.business.SystemEventTest
   </system-event-listener-class>
   <system-event-class>
      javax.faces.event.PostConstructApplicationEvent
   </system-event-class>              
 </system-event-listener>
</application>
</faces-config>

Everything is done now. Let's check the output directly. 

 

Output

Frequently Asked Questions

What is a JSF example?

A Java Web application framework called JavaServer Faces (JSF) is based on UI components. JSF is a server-based framework, and the JSF UI components' state and prescribed life cycles are represented on the server. The Java EE standard includes JSF.

What is a JSF lifecycle?

The interface between the user and the JSF application is provided by the Faces Controller servlet. The JSF Lifecycle, which governs the entire flow of events between user requests, sets the parameters within which it can work.What is the difference between JSF AND JSP?

  • A dynamic, platform-independent approach to constructing Web-based applications can be created using Java Server Pages (JSP), a server-side programming tool. 
  • Java Server Faces (JSF) is a Java-based online application framework designed to make it easier to integrate the development of web-based user interfaces.

What is MVC in java?

Model-View-Controller Pattern is also known as the MVC pattern. This pattern is employed to divide the concerns of an application.

Model 

View 

Controller

What is POJO?

Plain Old Java Object is referred to as POJO. It is an ordinary Java object that doesn't have any additional restrictions than those imposed by the Java Language Specification and doesn't need the classpath.

Conclusion

Finally, you've concluded this article. 

Congratulations!! You learned about  Application Events in JSF and ways to handle Application Events in this blog.

After reading these , are you eager to read more articles on the subject of JSF? Don't worry; Coding Ninjas will take care of everything. See the JSF for further information.

Please see our Code studio guided routes to learn more about DSA, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.

 

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass