previous | index | next

Value Change Event Example

Here is an application that changes its presentation depending upon which item is selected from a menu.

The selectOneMenu tag is given a valueChangeListener attribute that specifies a bean method to invoke when the menu's state changes:

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

Note that the JSF life cycle is not invoked by a form submission brought on by a button click.

Instead, the Javascript submit function causes the submission and eventually the countryChanged bean method is called:

   private static String US = "United States";
   ...
       public void countryChanged(ValueChangeEvent event) {
           FacesContext context = FacesContext.getCurrentInstance();
           if (US.equals((String) event.getNewValue()))
               context.getViewRoot().setLocale(Locale.US);
           else
               context.getViewRoot().setLocale(Locale.CANADA);
       }
   }

previous | index | next