By: Daniel Malcolm in JSF Tutorials on 2007年10月06日 [フレーム]
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in JSF )
How to open a new browser window from my JSF page?
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
Accessing Context Data in Beans using JSF
faces-config.xml to DirectTraffic in the JSF Application
JSF - TreeNode.setID gets IllegalArgument Exception
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
<convertNumber> and <convertDateTime> in JSF
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Latest Articles (in JSF)
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
<convertNumber> and <convertDateTime> in JSF
faces-config.xml to DirectTraffic in the JSF Application
JSF - TreeNode.setID gets IllegalArgument Exception
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
How to open a new browser window from my JSF page?
Accessing Context Data in Beans using JSF
Install and Deploy JBoss Application Server
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
<convertNumber> and <convertDateTime> in JSF
faces-config.xml to DirectTraffic in the JSF Application
JSF - TreeNode.setID gets IllegalArgument Exception
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
How to open a new browser window from my JSF page?
Accessing Context Data in Beans using JSF
Install and Deploy JBoss Application Server
Controlling Page Navigation in JSF - Static and Dynamic Navigation
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate