• 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
    • Join Us (JBC)
  • 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
    • Join Us (JBC)

How to implement f:phaseListener for h:commandButton?

July 8, 2008 //  by Krishna Srinivasan

Introduction

This article explains with basic example on how to implement the f:phaseListener in JSF. This tag is useful for registering any particular component to the Life Cycle of the phases.

also read:

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

This following program provides very basic example for using f:phaseListener inside h:commandButton.

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:phaseListener binding="#{jsfBean.phaseListenerImpl}" type="javabeat.jsf.PhaseListenerImpl"/>
                </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 PhaseListenerImpl phaseListenerImpl;

    public PhaseListenerImpl getPhaseListenerImpl() {
        return phaseListenerImpl;
    }

    public void setPhaseListenerImpl(PhaseListenerImpl phaseListenerImpl) {
        this.phaseListenerImpl = phaseListenerImpl;
    }

    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";
    }
}

PhaseListener Implementation (PhaseListenerImpl.java)

package javabeat.jsf;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
/**
 * source : www.javabeat.net
 */
public class PhaseListenerImpl implements PhaseListener{
    public void afterPhase(PhaseEvent event) {
        System.out.println("After Executing " + event.getPhaseId());
    }
    public void beforePhase(PhaseEvent event) {
        System.out.println("Before Executing " + event.getPhaseId());
    }
    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    }
}

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>

Output

Before Executing APPLY_REQUEST_VALUES 2
After Executing APPLY_REQUEST_VALUES 2
Before Executing PROCESS_VALIDATIONS 3
After Executing PROCESS_VALIDATIONS 3
Before Executing UPDATE_MODEL_VALUES 4
After Executing UPDATE_MODEL_VALUES 4
Before Executing INVOKE_APPLICATION 5
Test Value
After Executing INVOKE_APPLICATION 5
Before Executing RENDER_RESPONSE 6
After Executing RENDER_RESPONSE 6

Category: JSFTag: 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: « How to read xml file and inject bean reference using Spring Framework?
Next Post: Create simple custom Converter implementation class in JSF »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Write Comparators as Lambda Expressions in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

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