Listener tags in JSF

SHARE & COMMENT :

1) Introduction

JSF UI Components emit Events to Listeners if they are registered. The Event Handling Model in Java Server Faces is very similar to Java Beans Event Handling Model. The types of Events emitted will fall either in Action Event, Value-Change Event or Phase Event. For example, clicking a Button or a Hyperlink will cause the JSF Components to emit Action Events. Change in Value for a UI Input Component will make Value Change Events to happen. Phase Events will occur in any of the 6 Different Phases available in Request Processing Life-cycle.
Accordingly, 3 Listener Tags are identified for handling the various types of Events we discussed just before. They are mentioned below. Let us cover in detail in the subsequent sections.

  • Action Listener Tag
  • Value Change Listener Tag
  • Phase Listener Tag

2) Action Listener Tag

Components representing javax.faces.component.ActionSource are eligible to emit Action Events. Buttons (represented by <h:commandButton>) and Hyper-link (represented by <h:commandLink>) are the only two UI Components available in this category. The tag <f:actionListener> can be applied to these Action Source Components to attach a Listener. Let us look into the syntax of the Action Listener Tag.

<f:actionListener type = "ClassNameOfTheListener"/>

Let us see the various constraints imposed on the ‘type’ attribute. The first thing is that the Class must be a ActionListener, which means that it has to implement javax.faces.event.ActionListener interface and must provide a method called ActionListener.processAction(ActionEvent ActionEvent) which will hold the actual logic.
Following is the snippet code for attaching a Listener to the UI Components – Button and Hyper-link.

<h:commandButton value = "Submit">
<f:actionListerner type = "mylisteners.SubmitAction"/>
</h:commandButton>

<h:commandLink value = "Click Me">
<f:actionListerner type = "mylisteners.SubmitAction"/>
</h:commandLink>

The following is the code snippet for the Class implementing the ActionListener.
SubmitAction.java


package mylisteners;

import javax.faces.event.*;

public class SubmitAction implements ActionListener{

public void processAction(ActionEvent ActionEvent){
 // Actual Logic Here.
 }
 }

3) Value Change Listener Tag

All the Components that accept User Input Values are eligible to emit Value Change Events, which usually happens when the Value of the Input UI Control changes. Components like Text-Filed (<h:inputText>), Text-Area (<h:inputTextarea>), Hidden Text-Field (<h:inputHidden>), Password Field (<h:inputSecret>), Check-Box (<h:selectBooleanCheckbox>, <h:selectManyCheckbox>), List-Box (<h:selectManyCheckbox>, <h:selectManyListBox>) and Radio-Buttons (<h:selectOneRadio>) fall under this category.
Following is the syntax definition of the Value Change Listener Tag,

<f:valueChangeListener type = "ClassNameofTheListener"/>

The ‘type’ attribute must point to a Class that implements the javax.faces.event.ValueChangeListener interface along with the method ValueChangeListener.processValueChange(ValueChangeEvent vcEvent).
Let us look in the following sample Code for Input Text Field component that binds the Listener Class called MyValueChangeListener.

<h:inputTextField>
<f:valueChangeListener type = "listeners.MyValueChangeListener"/>
</h:inputTextField>

Following is the listing for the Value Change Listener Class.

MyValueChangeListener.java

<pre>package listeners;

import javax.faces.event.*;

public class MyValueChangeListener{

public void processValueChange(ValueChangeEvent vcEvent){

String oldValue = (String)vcEvent.getOldValue();
 String newValue = (String)vcEvent.getNewValue();

// Do Something with the old and the new value.
 }
 }

4) Phase Listener Tag

There is a major difference between Action/Value-Change Listener Tags and the Phase Listener Tags. In the case of Action/Value-Change Listener Tags, the Events will be emitted by the JSF UI Components which is in the Control of the Application Developer who writes Code for the Components, whereas Phase Events are emitted by the JSF Framework during the Request Processing Life-cycle. Remember that 6 Phases are involved in a JSF Request Processing Phase and during the beginning and the end of each phase, JSF Framework will emit Phase Events. We can handle the Events if we want to, thereby attaching Customized Logic in the Request Processing Life-cycle.

Let us look into the Syntax Definition of the Phase Listener Tag.

<f:phaseListener type = "ClassNameOfThePhaseListener"/>

The ‘type’ attribute refers to a class name that implements the javax.faces.event.PhaseEvent interface. Following is the code snippet for that,

MyPhaseListener.java

 package mylisteners;

import javax.faces.event.*;

public MyPhaseListener implements PhaseListener{

public void beforePhase(PhaseEvent PhaseEvent){
 }

public void afterPhase(PhaseEvent PhaseEvent){
 }

public PhaseId getPhaseId(){
 return PhaseId.ANY_PHASE;
 }
 }

From the above code, we can infer that the method PhaseListener.beforePhase() will be called before the beginning of a Phase and the method PhaseListener.afterPhase() will be called after the termination of the phase. Returning a value of PhaseId.ANY_PHASE in the method PhaseListener.getPhaseId() will cause the MyPhaseListener to be fired during each and every phase. Other legal return value for the PhaseListener.getPhaseId() are PhaseId.RESTORE_VIEW, PhaseId.APPLY_REQUEST_VALUES, PhaseId.PROCESS_VALIDATIONS, PhaseId.UPDATE_MODEL_VALUES, PhaseId.INVOKE_APPLICATION and PhaseId.RENDER_RESPONSE which are defined in javax.faces.event.PhaseId as Constants.

Attaching a Phase Listener to a View can be done by directly including the <f:phaseListener> tag within the JSF View. Consider the following code,

<f:view> 
<f:PhaseListener type = "mylisteners.MyPhaseListener"/> 
</f:view>

Javabeat has published many of the good articles related to Java Server Faces (JSF). This list will help you to learn the basic concepts on JSF and how to start using in your projects, please have a look on these articles when time permits. Don’t forget to leave your feedback on the comments section. This helps us to provide better content for you.

The above article would provide the higher level of understanding on each concept.  If you would like to know all the features in detail, please buy any of the JSF books listed in the book recommendations for Java Server Faces (JSF). Hope this helps you!!

If you would like to receive future mails on Java articles, please subscribe here.

Comments

comments

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.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better