HTML and CSS Reference
In-Depth Information
Listing 4-11. An Example of Value Change Listener
<h:outputLabel for="countries" value="Select a country: "/>
<h:selectOneMenu id="countries" value="#{}"
<f:selectItem itemLabel="---" itemValue="---"/>
<f:selectItem itemLabel="United States" itemValue="USA"/>
<f:selectItem itemLabel="Egypt" itemValue="Egypt"/>
<f:selectItem itemLabel="Denmark" itemValue="Denmark"/>
<h:commandButton value="Find Capital" /> <br/>
<h:outputText value="Capital of #{} is #{}"
rendered="#{ ne null}"/>
As we see in the bolded lines, we have a selectOneMenu component that has four items that are added
using <f:selectItem> tag. The first item represents no selection, while the rest of the items represent countries.
selectOneMenu component has a valueChangeListener attribute which includes a value change listener method
#{country.findCapital} . When the user selects one of the available countries and then clicks the CommandButton ,
then the form will be submitted and the value change listener method will be executed if the selectOneMenu value is
changed. Listing 4-12 shows the Country managed bean.
Listing 4-12. Country Managed Bean
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;
public class Country {
private String name;
private String capital;
public String getName() {
return name;
public void setName(String name) { = name;
public String getCapital() {
return capital;
public void setCapital(String capital) { = capital;
Search WWH ::

Custom Search