Hobione's Weblog

Living & Breathing in Web 2.0 Era

dataTable and radio button in JSF

Scenerio: Wanted to render the same radio button group for all rows in the table so that only one button can be selected at a time.  There seem to be no easy solution to that simple problem!

DataTable and RadioButton

Reason: The above plan apparently doesn’t work, because <h:selectOneRadio> renders an HTML “table” by itself, and that “messes up” the other “table” that the <h:dataTable> tag renders.

Solution:  One work around would be to use TomaHawk but instead of mixing two JSF implementations I went ahead to use command link.

Commnad Link2nd SoulutionThe key to getting this to work is to define the selectOneRadio component outside of the table. Then by setting the layout attribute to spread and using the ice:radio components inside the dataTable it gives you the rough layout/functionality.

<h2>Main Page</h2>
 <ice:panelGroup id="tablePanel">

 <ice:selectOneRadio id="trainingClassRadio"
 <f:selectItems value="#{testBean.radioItems}"/>

 <ice:dataTable var="class" value="#{testBean.trainingClassList}"
 <f:facet name="header">
 <ice:outputText value=""/>
 <ice:radio for="trainingClassRadio"

 <f:facet name="header">
 <ice:outputText value="Class Name"/>
 <ice:outputText value="#{class.className}"/>
 <f:facet name="header">
 <ice:outputText value="Class Number"/>
 <ice:outputText value="#{class.classNum}"/>
 <f:facet name="header">
 <ice:outputText value="Class Description"/>
 <ice:outputText value="#{class.classDesc}"/>

 <ice:panelGrid columns="2">
 <ice:outputText value="Selected Row - Class Name:"/>
 <ice:outputText value="#{testBean.selectedTraining.className}"/>
 <ice:outputText value="Selected Row - Class Number:"/>
 <ice:outputText value="#{testBean.selectedTraining.classNum}"/>
 <ice:outputText value="Selected Row - Class Description"/>
 <ice:outputText value="#{testBean.selectedTraining.classDesc}"/>


May 29, 2009 Posted by | Java Server Faces | 13 Comments

JSF: f:selectItem vs. javax.faces.model.SelectItem

Let me explain what works and then I will tell you what not works. ListModel has a list type of javax.faces.model.SelectItem:

public class TrainingSessionsListModel extends ListModel{

 private List<SelectItem> availableSlotSelectItems;

 public List<SelectItem> getSessionNumberSelectItems() {

 if (this.sessionNumberSelectItems == null){
    sessionNumberSelectItems = new ArrayList();
    for(int i = 100; i < 1000; i++){
       SelectItem selectItem = new SelectItem(i,Integer.toString(i));
 return sessionNumberSelectItems;

<p style="margin-bottom:12pt;">xhtml now referring to the above list model class to get the list and set the value to TrainingSessionsDTO.sessionNumber property:</p>

 <ice:selectOneMenu id="sessionNumber" partialSubmit="true" value="#{
trainingSessionsListModel.TrainingSessionsDTO.sessionNumber}" required="true">

<f:selectItem itemLabel="#{msgs.select}" itemValue="" />
 <f:selectItems value="#{trainingSessionsListModel.sessionNumberSelectItems}"/>


Here is the transfer object with getter and setter.

 public class TrainingSessionsDTO extends TransferSupport implements Transfer,
 Serializable {

 String sessionNumber;

 //getter and setter .......



The above works fine, no problem but if I change the xhtml to these it wont work.

<ice:selectOneMenu id="sessionNumber" partialSubmit="true" value="#{
trainingSessionsListModel.TrainingSessionsDTO.sessionNumber}" required="true">
 <f:selectItem itemLabel="#{msgs.select}" itemValue="" />

 <f:selectItem itemLabel="100" value="100" />
 <f:selectItem itemLabel="200" value="200" />
 <f:selectItem itemLabel="300" value="300" />

java.lang.String cannot be cast to javax.faces.model.SelectItem


<f:selectItem itemLabel="100" itemValue="100" />
<f:selectItem itemLabel="200" itemValue="200" />
<f:selectItem itemLabel="300" itemValue="300" />

The value is meant to send in a SelectItem object and not a String. This should eliminate the exception you are seeing. Here is my beautiful finished work (form and datatable):

May 1, 2009 Posted by | Java Server Faces | 4 Comments