• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

How to implement ActionListener (f:actionListener) in JSF?

July 5, 2008 //  by Krishna Srinivasan

Introduction

This article explains how to implement the ActionListener class in the JSF core tag library.

also read:

  • Introduction to JSF
  • JSF Interview Questions
  • Request Processing Lifecycle phases in JSF

An ActionListener is an event handler interface for a class that can respond to user events in your JSF page. The body content of this tag must be empty.

JSP File (index.jsp)

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
    <body>
        <f:view>
            <h:form>
               <h:inputText value="#{jsfBean.field}"/>
               <h:commandButton action="#{jsfBean.submit}" value="Submit">
                    <f:actionListener binding="#{jsfBean.actionListenerImpl}" type="javabeat.jsf.ActionListenerImpl"/>
                </h:commandButton>
            </h:form>
        </f:view>
    </body>
</html>

JavaBean (JavaBeatJsfBean.java)

package javabeat.jsf;

import javax.faces.event.ActionEvent;

/**
 * source : www.javabeat.net
 */
public class JavaBeatJsfBean {

    private String field;
    private ActionListenerImpl actionListenerImpl;

    public ActionListenerImpl getActionListenerImpl() {
        return actionListenerImpl;
    }

    public void setActionListenerImpl(ActionListenerImpl actionListenerImpl) {
        this.actionListenerImpl = actionListenerImpl;
    }
    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }
    public void action() {
        System.out.println("Inside Action Listener");
    }

    public void action(ActionEvent event) {
        System.out.println("Inside Action Listener");
    }

    public String submit() {
        System.out.println(this.field);
        return "success";
    }
}

ActionListener implementation class (ActionListenerImpl.java)

package javabeat.jsf;

import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

public class ActionListenerImpl implements ActionListener {

    public void processAction(ActionEvent event)
            throws AbortProcessingException {
        System.out.println("Inside Process Action");
    }
}

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<faces-config version="1.2"
    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-facesconfig_1_2.xsd">

    <managed-bean>
    <managed-bean-name>jsfBean</managed-bean-name>
    <managed-bean-class>javabeat.jsf.JavaBeatJsfBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>


</faces-config>

Category: JavaTag: JSF Tags

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Entity Beans in EJB(BMP)
Next Post: Basic steps to configure Log4j using xml and properties file »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact