Calling Multiple Listeners in JSF

By: Daniel Malcolm Emailed: 1588 times Printed: 2038 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

There may be situations in which you want multiple value change listeners or multiple action listeners to respond to an event. In that case, you need a slightly different syntax to attach the listeners to the JSF element.

While using Action Listener, a value is attached to a change listener or an action listener to a 
JSF element by using the valueChangeListener or actionListener attributes. When using the attribute syntax, you use a method-binding expression to bind a method as a listener. However, this works only when attaching a single listener.

When you need to attach multiple listeners, there is a JSF tag for value change listeners and one for action listeners that can be used to attach one or more listeners to an element. When using the tag syntax, you will be specifying a class that implements a listener interface. For value change listeners, the tag is <f:valueChangeListener>, with an attribute named type that is the class name of the listener. The interface that the listener must implement is javax.faces.event.ValueChangeListener. It has a single method that must be implemented:

void processValueChangeEvent(ValueChangeEvent);

So, if we assume that the FlightSearch and FlightTypes classes both implement ValueChangeListener, we could attach both of them to an element like this:

<h:selectOneRadio layout="lineDirection"
value="#{flight.tripType}"
onclick="submit()"
immediate="true">
<f:valueChangeListener type="com.apress.projsp.FlightSearch"/>
<f:valueChangeListener type="com.apress.projsp.FlightTypes"/>
<f:selectItems value="#{types.tripTypes}"/>
</h:selectOneRadio>

For action listeners, the tag is <f:actionListener>, with an attribute named type that is the class name of the listener. The interface that the listener must implement is javax.faces.event.ActionListener. It has a single method that must be implemented:

void processAction(ActionEvent);

If we assume that the FlightSearch and FlightTypes classes both implement ActionListener, we could attach both of them to an element, like this:

<h:commandButton value="Search"
action="#{flight.search}">
<f:actionListener type="com.apress.projsp.FlightSearch"/>
<f:actionListener type="com.apress.projsp.FlightTypes"/>
</h:commandButton>

In either of the two preceding examples, both listeners will be called at the appropriate time to respond to the event. If any listeners are attached by using attribute syntax, they will be called first. Then any listeners attached by using tag syntax will be called, in order of declaration.


JSF Home | All JSF Tutorials | Latest JSF Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

On my blog (http://www.future-edge.nl/blog/adding-multiple-action-listeners-to-a-command-link) a user asked me why i didn't use the tag referred to in this article. We didn't use it because we wanted to bind an arbitrary method (from a backing bean) as an action listener (like the actionListener attribute on the commandButton) and not an object that implements ActionListener.

View Tutorial          By: Robert Willems at 2011-11-29 06:26:21

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Daniel Malcolm
javac options in Java
Operator Precedence in Java
Calling Multiple Listeners in JSF
Using free() Function in C
ForwardAction in Struts
Listing Files and Directories sample program in C
Binary Tree - (Self-referential Structures) example program in C
A simple program using EL in JSP
Command-line Arguments in C
Example Calculator program in C - describing use of External Variables in C
Assignment Operators and Expressions in C
The for statement in C
JSF Basics
assert() Versus Exceptions in C++
RMS Basics in J2ME

More Tutorials in JSF
Struts Vs JSF (A comparison of Struts against JSF)
faces-config.xml to DirectTraffic in the JSF Application
<convertNumber> and <convertDateTime> in JSF
Action listeners in JSF
Calling Multiple Listeners in JSF
Using Javascript in JSF
How to open a new browser window from my JSF page?
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
JSF - TreeNode.setID gets IllegalArgument Exception
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle

More Latest News
Most Viewed Articles (in JSF )
How to open a new browser window from my JSF page?
Install and Deploy JBoss Application Server
Java Bean Scopes in JSF
Action listeners in JSF
Calling Multiple Listeners in JSF
Using Javascript in JSF
Struts Vs JSF (A comparison of Struts against JSF)
What is JSF (JavaServer Faces)?
<convertNumber> and <convertDateTime> in JSF
JSF Life Cycle
Accessing Context Data in Beans using JSF
JSF Basics
Differences between JavaServer Faces technology and Struts
faces-config.xml to DirectTraffic in the JSF Application
Installing JSF
Most Emailed Articles (in JSF)
JSF Basics
JSF Life Cycle
How to open a new browser window from my JSF page?
<convertNumber> and <convertDateTime> in JSF
Installing JSF
faces-config.xml to DirectTraffic in the JSF Application
Action listeners in JSF
Calling Multiple Listeners in JSF
Struts Vs JSF (A comparison of Struts against JSF)
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Using Javascript in JSF
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
Java Bean Scopes in JSF