Jsf actionlistener event example

JSF fired action events when user clicks on a button or link component like h:commandButton or h:commandLink etc.

An action event can be handled by following ways:

1. Method Binding.
2. ActionListener Interface.

Method Binding:

Use the managed bean method directly into actionListener attribute of the corresponding JSF component.

Bean class:

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

JSF component:

<h:commandButton id="submitButton" 
		value="Submit"
               	actionListener="#{ testBean.actionEventMethod}" >
</h:commandButton>

ActionListener Interface:

Creates a class which implements ActionListener interface and override the processAction() method. Pass it into f:actionListener tag of the corresponding JSF component.

Java class:

public class ActionListenerTestClass implements ActionListener { 
	@Override
	public void processAction(ActionEvent event)
			throws AbortProcessingException { 
		//method body 
	}
}

JSF component:

<h:commandButton id="submitButton" 	value="Submit">
               	<f:actionListener type="ActionListenerTestClass" />
</h:commandButton>

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 action listener example.</title>
	</h:head>
    <h:body>
    	<h2>JSF action listener example.</h2>
	<h:form> 
		Method Binding Test:
		<br/> 
        <h:commandButton value="Submit" 
         action="success"
         actionListener="#{test.messageChanged}">
      </h:commandButton>
      <br/><br/>
      f:actionListener Test: 
      <br/>
      <h:commandButton value="Submit" action="success" >
         <f:actionListener 
            type="com.javawithease.business.ActionListenerTest" />
      </h:commandButton>
	</h:form>
 
</h:body>
 
</html>

Test.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
 
/**
 * Managed bean. 
 * @author javawithease
 */
@ManagedBean(name = "test")
@SessionScoped
public class Test {
	private String message = "Hello javawithease";
 
	public void messageChanged(ActionEvent actionEvent) {
		message = "Action Listener Test " +
				"using method binding.";
	}
 
	public String getMessage() {
		return message;
	}
 
	public void setMessage(String message) {
		this.message = message;
	}
 
}

ActionListenerTest.java

import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
 
public class ActionListenerTest implements ActionListener {
	 public void processAction(ActionEvent actionEvent) 
	   throws AbortProcessingException {
	      Test test = (Test) FacesContext.getCurrentInstance().
	         getExternalContext().getSessionMap().get("test"); 
	      test.setMessage("Action Listener Test" +
	      		" using f:actionListener.");
	   }
}

welcome.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 action listener example.</title>
	</h:head>
    <h:body>
    	<h2>JSF action listener example.</h2> 
    	<h3><h:outputText value="#{test.message}"/></h3> 	
    </h:body>
 
</html>

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">
 
<navigation-rule>
   <from-view-id>test.xhtml</from-view-id>
   <navigation-case>
      <from-outcome>success</from-outcome>
      <to-view-id>welcome.xhtml</to-view-id>
   </navigation-case>   
</navigation-rule>
 
</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/JSFExample39/faces/test.xhtml

Output:

JSF example39-1
 
Click Submit button for Method binding.
JSF example39-2
 
Click Submit button for f:actionListener.
JSF example39-3
 
Download this example.