JSF 2 ActionListener Attribute and Class Example
January 21, 2015
In this page we will learn ActionListener attribute and class for registering ActionListener in components. Before any action, ActionListener is called to perform required task. To use ActionListener in UI, there is an attribute as actionListener and a tag as f:actionListener that can be used with h:commandButton. If using ActionListener as an attribute, we need to create a method in managed bean which will be referred with ActionListener attribute. Method has ActionEvent argument to access required values. If using ActionListener as a tag, we need to create a class that will implement ActionListener interface. There is a method as processAction, which needs to be overridden and method has ActionEvent argument to access required values.
ActionListener as an Attribute
To use ActionListener, JSF 2 has provided actionListener attribute. We can use the attribute with h:commandButton as below.<h:commandButton id="commandButton" action="#{studentBean.submitActionForMethod}" value="submit" actionListener="#{studentBean.performAction}"> </h:commandButton>
ActionListener as a Class
To use ActionListener as a class, we need to use f:actionListener tag which has a type element . Find the code snippet as below.<h:commandButton id="commandButton" action="#{studentBean.submitActionForClass}" value="submit"> <f:actionListener type="com.concretepage.SubmitActionListener" /> </h:commandButton>
Complete Example for ActionListener
For the ActionListener demo, we are creating a simple application. We have two webpage, one for attribute and one for tag demo. When we will click on submit button, there will be output in console.Project Structure in Eclipse
Find the project structure in eclipse.
Managed Bean
Find the managed bean being used in the example.StudentBean.java
package com.concretepage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.event.AbortProcessingException; import javax.faces.event.ActionEvent; @ManagedBean(name = "studentBean", eager = true) @RequestScoped public class StudentBean { public void performAction(ActionEvent event) throws AbortProcessingException { System.out.println("Form Id by ActionListener attribute:"+event.getComponent().getParent().getId()); } public String submitActionForMethod() throws AbortProcessingException { System.out.println("Action Submitted for ActionListener attribute."); return "eventwithmethod"; } public String submitActionForClass() throws AbortProcessingException { System.out.println("Action Submitted for ActionListener class."); return "eventwithclass"; } }
Implementation of ActionListener Interface
Find the implementation of ActionListener interface.SubmitActionListener.java
package com.concretepage; import javax.faces.event.AbortProcessingException; import javax.faces.event.ActionEvent; import javax.faces.event.ActionListener; public class SubmitActionListener implements ActionListener { @Override public void processAction(ActionEvent event) throws AbortProcessingException { System.out.println("Form Id by ActionListener class:"+event.getComponent().getParent().getId()); } }
UI for actionListener attribute and f:actionListener Tag
Find the XHTML for ActionListener attribute demo.eventwithmethod.xhtml
<html lang="en" 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>ActionListener with Method in JSF 2</title> </h:head> <h:body> <h3>ActionListener with Method in JSF 2</h3> <h:form id="studentForm"> <h:commandButton id="commandButton" action="#{studentBean.submitActionForMethod}" value="submit" actionListener="#{studentBean.performAction}"> </h:commandButton> </h:form> </h:body> </html>
eventwithclass.xhtml
<html lang="en" 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>ActionListener with Class in JSF 2</title> </h:head> <h:body> <h3>ActionListener with Class in JSF 2</h3> <h:form id="studentForm"> <h:commandButton id="commandButton" action="#{studentBean.submitActionForClass}" value="submit"> <f:actionListener type="com.concretepage.SubmitActionListener" /> </h:commandButton> </h:form> </h:body> </html>
Output
Access the URL http://localhost:8080/JSF2Demo-1/eventwithmethod.xhtml for ActionListener attribute demo and you will get web page as below.
Form Id by ActionListener attribute:studentForm Action Submitted for ActionListener attribute.

Form Id by ActionListener class:studentForm Action Submitted for ActionListener class.