Table of contents
1.
Introduction
2.
JSF Action Listener
2.1.
Method Binding
2.2.
Define a method
2.3.
Action Listener
3.
Example Application of ActionListener
3.1.
UserData.java
3.2.
UserActionListener.java
3.3.
home.xhtml
3.4.
result.xhtml
3.5.
Output
4.
Frequently Asked Questions
4.1.
Explain JSF and mention its uses.
4.2.
Explain Listener in JSF. 
4.3.
Distinguish between an action and an actionListener in JSF.
4.4.
Define the JSF Tag library.
4.5.
Why do we use an action listener?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

JSF actionListener

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

Introduction

Action listeners are offered to simplify handling action events in Java server faces. An event is called, for instance, when a button or hyperlink is clicked on a page. However, because it lacks access to the user interface's state, that event handler is constrained in what it can do. They can be employed for more effective event handling because they receive information about the user interface.

In this article, we shall comprehend the concept of action listeners in JSF and how it can be used to handle certain action events when a user clicks on a link like the h:commandButton or h:link.

JSF Action Listener

The JSF fires action events that can be handled in one of two ways when a user interacts with components like h:commandButton or h:link. They are

Method Binding

A JSF component, such as a button, can be linked to a method of a Java class using method binding. Here, we use the managed bean function with the matching UI component's actionListener attribute.

Define a method

public void updateData(ActionEvent e) {
            data = "Coding Ninjas";
 }
<h:commandButton id = "submitButton" 
   value = "Submit" action = "#{userData.showResult}"
   actionListener = "#{userData.updateData}" />
</h:commandButton>                                          
You can also try this code with Online Java Compiler
Run Code

Action Listener

The processAction() method is overridden in a class that implements the interface. Give the associated JSF component's c:actionListener tag the class name.

 

Implement ActionListener

public class UserActionListener implements ActionListener {
   @Override
   public void processAction(ActionEvent arg0)
   throws AbortProcessingException { 
      //access userData bean directly
      UserData userData = (UserData) FacesContext.getCurrentInstance().
      getExternalContext().getSessionMap().get("userData"); 
      userData.setData("Coding Ninjas");
   }
}
You can also try this code with Online Java Compiler
Run Code

 

Use listener method

<h:commandButton id = "submitButton1" 
   value = "Submit" action = "#{userData.showResult}" >
   <f:actionListener type = "com.codingninjas.test.UserActionListener" />
</h:commandButton>

Example Application of ActionListener

To test the actionListener in JSF, let's build a test JSF application.

application of actionListener


UserData.java

package com.codingninjas.test;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
   private static final long serialVersionUID = 1L;
   private static Map<String,String> countryMap;
   private String data = "sample data";
   public String showResult() {
      return "output";
   }
   public void updateData(ActionEvent e) {
      data="Coding Ninjas";
   }
   public String getData() {
      return data;
   }
   public void setData(String data) {
      this.data = data;
   }
}
You can also try this code with Online Java Compiler
Run Code

 

UserActionListener.java

package com.tutorialspoint.test;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
public class UserActionListener implements ActionListener {
   @Override
   public void processAction(ActionEvent arg0) 
   throws AbortProcessingException {
      //access userData bean directly
      UserData userData = (UserData) FacesContext.getCurrentInstance().
      getExternalContext().getSessionMap().get("userData"); 
      userData.setData("Coding Ninjas");
   }
}
You can also try this code with Online Java Compiler
Run Code

 

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 ActionListener</title>
   </h:head>
   <h:body>
      <h2>actionListener Example</h2>
      <h:form>
         </h:commandButton>
         <h2>ActionListener interface</h2>
         <hr/>
         <h:commandButton id = "submitButton1"
            value = "output" action = "#{userData.showResult}" >
            <f:actionListener
               type = "com.codingninjas.test.UserActionListener" />
         </h:commandButton>
      </h:form>
   </h:body>
</html>

result.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:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/jsf/html"> 
   <h:head>
      <title>JSF Actionlistner</title>   
   </h:head> 
   <h:body>
      <h2>Result</h2>
      <hr />
      #{userData.data}
   </h:body>
</html>  

Output

output
output

Frequently Asked Questions

Explain JSF and mention its uses.

A new, industry-standard Java framework for creating Web applications is called JavaServer Faces (JSF).  It streamlines development by offering a component-centric method for creating Java Web user interfaces.

Explain Listener in JSF. 

The usage of listeners allows users to listen to on-page events and take the appropriate action. Classes are one type of listener that an application developer might use, such as managed bean techniques.

Distinguish between an action and an actionListener in JSF.

Action: used to carry out some logic and switch to another page. String or null are the results. Whereas actionListener: Used to carry out business logic and maintain consistency.

Define the JSF Tag library.

JSF offers a standard HTML tag library. These tags are translated into the appropriate HTML output. The following URI namespaces in an HTML node must be used for these tags.

Why do we use an action listener?

When a button or link component, such as an h:commandButton or an h:commandLink, is clicked, "Action Events" are triggered in JSF. Therefore,  action listeners are used for UI interface logic, and action invokes observation.

Conclusion

This article covered the topic of action listeners in JSF, what it is, how it is implemented, and why it is used. We also discussed an example to understand the concept of action listener better.

Explore our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more. Also, you may consider our paid courses to give your career an edge over others.

Do upvote our blogs if you find them helpful and engaging!

Happy coding, ninjas!

Live masterclass