f:valueChangeListener |
|
The ValueChangeListener tag registers a ValueChangeListener instance on the
component associated with the enclosing tag. The ValueChangeListener interface
should be implemented by classes that you want to register with components that
publish value change events. Any component that receives user input, such as one
of the HTML select or text input components, can publish value change events. A
component fires a value change event when its input changes, but only if the new
input is validated successfully. You can register several ValueChangeListeners
with a component and they will be invoked in the order that they are
registered.
An alternative to this tag is to use a method-binding
expression pointing at a value change listener method of a backing bean on the
component tag itself. Notice in the example below the use of the JavaScript
onchange() event to trigger form submission when the list selection changes.
Without this JavaScript event, the user must manually submit the form to invoke
the ValueChangeListener.
JSF Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head />
<body>
<h:form>
<h:selectOneMenu value="#{customerController.customer.homeAddress.country}"
converter="countryConverter" id="countryList">
<f:selectItems value="#{countryController.countries}" var="country"
itemLabel="#{country.name}" itemValue="#{country}" />
<f:valueChangeListener type="com.mycompany.MyValueChangeListener" />
<f:ajax />
</h:selectOneMenu>
</h:form>
</body>
</html>
This example was formatted by JSFToolbox for Dreamweaver.
Java Code
package com.mycompany;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
public class MyValueChangeListener implements ValueChangeListener {
@Override
public void processValueChange(ValueChangeEvent event)
throws AbortProcessingException {
System.out.println("Value changed");
}
}
Rendered Output
|