JSF Tutorial - JSF ActionListener Example








We can handle user click event for as h:commandButton or h:link.

To register event handler, we can pass the name of the managed bean method in actionListener attribute of UI Component.

Or we can choose to implement ActionListener interface and pass the implementation class name to actionListener attribute of UI Component.

The following code shows how to add user-defined a method to actionListener attribute from h:commandButton.

public void updateData(ActionEvent e){
   data="Hello World";
}

Use above method

<h:commandButton id="submitButton" 
   value="Submit" action="#{userData.showResult}"
   actionListener="#{userData.updateData}" />
</h:commandButton>

The following code shows how to implement ActionListener and use f:actionListener tag.

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("Hello World");
   }
}

Use listener method

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




Example

The following code is from MyActionListener.java.

package com.java2s.common;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

public class MyActionListener implements ActionListener{

  @Override
  public void processAction(ActionEvent event)
      throws AbortProcessingException {
    
    System.out.println("Any use case here?");
  
  }
  
}

The following code is from 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:h="http://java.sun.com/jsf/html"
      >
    <h:body>
    #{normal.buttonId}
    </h:body>
</html>

The following code is from demo.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"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:body>
    <h:form id="form">
      <ui:remove>  
      <h:commandButton id="submitButton" 
        value="Submit" action="#{normal.outcome}" 
        actionListener="#{normal.printIt}" />
      </ui:remove>
      <h:commandButton id="submitButton" 
        value="Submit" action="#{normal.outcome}" >
        <f:actionListener type="com.java2s.common.MyActionListener" />
      </h:commandButton>
    </h:form>
    </h:body>
</html>

The following code is from UserBean.java.

package com.java2s.common;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
 
@ManagedBean(name="normal")
@SessionScoped
public class UserBean implements java.io.Serializable{

  public String buttonId="java2s.com"; 
  
  public String getButtonId() {
    return buttonId;
  }

  public void setButtonId(String buttonId) {
    this.buttonId = buttonId;
  }

  public void printIt(ActionEvent event){
    //Get submit button id
    buttonId = event.getComponent().getClientId();
  }
  
  public String outcome(){
    return "result";
  }
}


Download ActionListener.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml