Do you think IIT Guwahati certified course can help you in your career?
No
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
}
}
creates a class that replaces the processEvent() method and implements the SystemEventListener interface. Add a line for it to the faces-config.xml file.
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;
}
}
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.