Pro JSF and Ajax
Code Sample 1-4.
Backing Bean Using the HtmlSelectOneRadio Subclass
package com.apress.projsf.ch1.application;
import javax.faces.event.ActionEvent;
import javax.faces.component.html.HtmlSelectOneRadio;
public class SampleBean
{
public void onAction(
ActionEvent event)
{
_selectOneRadio.setValue("java.dude");
}
public void setSelectOneRadio(
HtmlSelectOneRadio selectOneRadio)
{
_selectOneRadio = selectOneRadio;
}
public HtmlSelectOneRadio getSelectOneRadio()
{
return _selectOneRadio;
}
private HtmlSelectOneRadio _selectOneRadio;
}
In Code Sample 1-4, the managed bean is using the renderer-specific subclass
HtmlSelectOneRadio
. If application developers want to change the UI and replace the
selectOneRadio
component with a selectOneMenu component in the page, a class cast
exception is thrown at runtime. The application developer can avoid this by instead using
the parent class of the selectOneRadio component--UISelectOne. Code Sample 1-5 shows
how the page and the managed bean source look with the recommended approach.
C H A P T E R 1
s
T H E F O U N D AT I O N O F J S F : C O M P O N E N T S
18