Monday, June 16, 2008

JSF "immediate"events

JSF supports three kinds of events:
  • Value change events
  • Action events
  • Phase events

Value change events are normally fired after the Process Validations phase.
Action events are normally fired after the Invoke Application phase.
But sometimes you want value change events or action events to fire at the beginning of the life cycle to bypass validation for one or more components.

Immediate events are fired after the Apply Request Values phase.

<h:selectOneMenu value="#{}" onchange="submit()" immediate="true"
<f:selectItems value="#{form.countryNames}"/>

With the immediate attribute set to true, the method form.coutryChanged() was invoked after "Apply Request Values".

public void countryChanged(ValueChangeEvent event) {
FacesContext context = FacesContext.getCurrentInstance();

if (US.equals((String) event.getNewValue()))


The call to renderResponse() skips the rest of the life cycle—including validation of the rest of the input components in the form—up to Render Response. Otherwise, the validations will still be performed and the validation error will still be displayed.

One more thing is noteworthy about this example. Notice that we add an onchange attribute whose value is submit() to our h:selectOneMenu tag. That form submit is crucial because the JSF implementation handles all events on the server. If you take out the onchange attribute, the form will not be submitted when the selected menu item is changed, meaning that the JSF life cycle will never be invoked, our value change listener will never be called.

Refer to JSF application life cycle.

No comments:

Post a Comment