Triggering an Event by Changing a Value

When the user changes the value of an input component, a ValueChangeEvent is fired when the form is submitted. You have similar options for handling a ValueChangeEvent as you have for an ActionEvent: use a value change listener method binding or attach one or more ValueChangeListener to the component:

<!-- Using a method binding expression -->
<h:selectBooleanCheckbox valueChangeListener="#{myBean.handleNewValue}" />

<!-- Using one or more listeners -->
<h:selectBooleanCheckbox>
  <f:valueChangeListener type="com.mycompany.LogEventListener" />
  <f:valueChangeListener type="com.mycompany.HandleNewValueListener" />
</h:selectBooleanCheckbox>

Two things make the JSF ValueChangeEvent less important than similar events in a traditional GUI framework: it fires only when the form is submitted (because that’s when the server can detect the change), and JSF’s mechanisms for validation and automatic model updates handle most of the typical use cases. That said, it’s still useful for some features, especially if you know that your users use JavaScript-enabled browsers and are connected to the server via a high-speed connection so frequent requests don’t cause performance problems.

In this section, we look at how to use a ValueChangeEvent triggered by a change in a checkbox value to switch between the standard and extended report entry type choices. Example 8-6 shows the entry form page modified for this purpose.

Example 8-6. Entry form area JSP page with ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.