jsfjsf-2managed-beanselectmanycheckbox

f:selectItems is not rendering checkboxes in view


I am trying to render a few check boxes by using f:selectItems but web page is just not displaying check boxes as expected. Please look at the code below and let me know that what I am missing.

HTML:

    <h:form  prependId="false">
        <table align="left" cellspacing="5">
            <tr>
                <td align="right" valign="top"><h:outputText value="#{msgs.interests}" /></td>
                <td align="left" valign="top">
                    <h:selectManyCheckbox value="#{testBean.interests}">
                        <f:selectItems value="#{testBean.checkBoxItems}" />
                    </h:selectManyCheckbox>
                </td>
            </tr>
            <tr>
                <td align="right" valign="top"><h:commandButton value="#{msgs.save}" /></td>
                <td align="left" valign="top"><h:commandButton value="#{msgs.cancel}" /></td>
            </tr>
        </table>
    </h:form>

Managed Bean:

package com.jsf.ManagedBeans;

import javax.inject.Named; import javax.faces.bean.RequestScoped;

import javax.faces.model.SelectItem;

@Named("testBean") @RequestScoped public class TestBean {

private int[] interests;
private SelectItem[] checkBoxItems = {
    new SelectItem("Dancing", "Dancing"),
    new SelectItem("Singing", "Singing"),
    new SelectItem("Reading", "Reading"),
    new SelectItem("Writing", "Writing")
};

public SelectItem[] getCheckBoxItems() {

    return checkBoxItems; 
}

public int[] getInterests() {     return interests;
}
public void setInterests(int[] newValue) { 
    interests = newValue; 
} }

Solution

  • You'll want to use CDI scope annotations with the CDI @Named annotation and also make sure CDI is enabled for the project (WEB-INF/beans.xml file exists).

    Aside from that, add a debug statement to a PostConstruct method to confirm that the bean is being created. That method is run automatically by CDI after a bean is constructed. It can also be used to initialize bean fields as it is guaranteed to run once and only once per instantiation, unlike the class constructor.

    package com.jsf.ManagedBeans;
    
    import javax.inject.Named; 
    import javax.enterprise.context.RequestScoped;
    
    import javax.faces.model.SelectItem;
    
    @Named @RequestScoped public class TestBean {
    
      private int[] interests;
      private SelectItem[] checkBoxItems = {
        new SelectItem("Dancing", "Dancing"),
        new SelectItem("Singing", "Singing"),
        new SelectItem("Reading", "Reading"),
        new SelectItem("Writing", "Writing")
      };
    
      @PostConstruct public void init() {
          System.out.println("testBean initialized");
      }
    
      public SelectItem[] getCheckBoxItems() {
    
        return checkBoxItems; 
      }
    
      public int[] getInterests() {     return interests;
      }
      public void setInterests(int[] newValue) { 
        interests = newValue; 
      }   
    }