HTML and CSS Reference
In-Depth Information
public void beverageSelected(ValueChangeEvent event) {
String selectedBeverage = event.getNewValue().toString();
if ("tea".equals(selectedBeverage)) {
price = 2.0;
} else if ("coffee".equals(selectedBeverage)) {
price = 2.5;
} else if ("cocacola".equals(selectedBeverage)) {
price = 3.0;
}
}
}
As shown in Listing 2-22, the method of the
valueChangeListener
attribute has the following signature:
void
.
•
Returns
ValueChangeEvent
object.
The
ValueChangeEvent
object holds both the old value and the new selected value. The new selected value can
be retrieved using
getNewValue()
. In the
beverageSelected
method, the new selected value is retrieved and a price is
set according to the selected beverage item. Figure
2-3
shows the beverages example output.
•
Takes a single argument which is the
Figure 2-3.
The beverages example output
So far, we have seen two examples of the JSF method expressions; in the following chapters, we will see many
other examples.
■
You do not have make a full page submission in order to execute the
valueChangeListener
method of the
EditableValueHolder
; otherwise, you can use the
<f:ajax>
tag in order to invoke the
valueChangeListener
method in
an ajaxified style (the
<f:ajax>
tag will be discussed in detail in Chapter 5).
Note
It is important to note that if you are working on a Java EE 6 container (or later) which includes the Unified EL
2.1, you can invoke arbitrary methods with parameters. Let's see an example to explain this. Listing 2-23 shows the
calculateAverage
method of a custom
Maths
managed bean.
Listing 2-23.
The calculateAverage Method of a Custom Maths Managed Bean
@ManagedBean
@RequestScoped
public class Maths {
public Double calculateAverage (Double number1, Double number2) {
return (number1 + number2) / 2;
}
}

