How to use h:selectBooleanCheckbox, h:selectManyCheckbox, h:selectOneRadio JSF 2.0 example


      Click here to attend Spring Framework 4.x and certification course with a discount

Use the following example project for using
h:selectBooleanCheckbox,
h:selectManyCheckbox,
h:selectOneRadio

tags inside a JSF project

<?xml version=’1.0′ encoding=’UTF-8′ ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221;
      xmlns:h=”http://java.sun.com/jsf/html&#8221;
      xmlns:f=”http://java.sun.com/jsf/core”&gt;
    <h:head>
        <title>h:selectBooleanCheckbox – h:selectManyCheckbox – h:selectOneRadio example</title>
    </h:head>
    <h:body style=”background-color: lightskyblue”>
        <h1>h:selectBooleanCheckbox <br></br>
            h:selectManyCheckbox <br></br>
            h:selectOneRadio example JSF 2.0 project</h1>
        <h:form>            
            <h2>h:selectBooleanCheckbox example</h2>
            <h:selectBooleanCheckbox id=”checkboxApproval” value=”#{ContentGenerator.selectedValueBooleanCheckbox}”/>
            <h:outputText value=”#{ContentGenerator.checkboxApprovalText}”/>    
            <h2>h:selectManyCheckbox example</h2>
            <h:selectManyCheckbox id=”manyCheckBoxes”  value=”#{ContentGenerator.selectedItemsManyCheckBox}” >
                <f:selectItems value=”#{ContentGenerator.checkBoxes}” />
            </h:selectManyCheckbox>           
            <h2>h:selectOneRadio example</h2>
            <h:selectOneRadio id=”selectOneRadio” value=”#{ContentGenerator.selectedItemValueSelectOneRadio}”>
                <f:selectItems value=”#{ContentGenerator.radios}” />
            </h:selectOneRadio>           
            <h:commandButton action=”#{ContentGenerator.calledByHtmlJSFCommanButton}” value=”Produce ouput” />           
            <h:panelGroup rendered=”#{ContentGenerator.renderOutput}”>
                <br></br>
                <br></br>
                h:selectBooleanCheckbox — I understand is selected –>
                <b><h:outputText value=”#{ContentGenerator.selectedValueBooleanCheckbox}” /></b>
                <br></br>
                <br></br>
                h:selectManyCheckbox — selected values –>
                <b><h:outputText value=”#{ContentGenerator.selectedItemValuesManyCheckBox}” /></b>
                <br></br>
                <br></br>
                h:selectOneRadio — selected value –>
                <b><h:outputText value=”#{ContentGenerator.selectedItemValueSelectOneRadio}” /></b>
            </h:panelGroup>            
        </h:form>
    </h:body>
</html>

Source code for generating content for h:selectManyCheckbox and h:selectOneRadio JSF components

package tagscontentgenerator;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author tunatore
 */
@ManagedBean(name=”ContentGenerator”)
@RequestScoped
public class ContentGenerator {

    /** Creates a new instance of ContentGenerator */
    private boolean renderOutput = false;
    private boolean selectedValueBooleanCheckbox = false;

    private String selectedItemValuesManyCheckBox = “”;
    private String selectedItemValueSelectOneRadio = “”;
    private String checkboxApprovalText = “I understand!”;  

    private List<String> selectedItemsManyCheckBox = null;
    private List<SelectItem> checkBoxes = null;
    private List<SelectItem> radios = null;

public ContentGenerator() {

        checkBoxes = new ArrayList<SelectItem>();       
        SelectItem item1 = new SelectItem(“Subaru”, “Subaru”);
        SelectItem item2 = new SelectItem(“Honda”, “Honda”);
        SelectItem item3 = new SelectItem(“Toyota”, “Toyota”);
        SelectItem item4 = new SelectItem(“Ford”, “Ford”);
        checkBoxes.add(item1);
        checkBoxes.add(item2);
        checkBoxes.add(item3);
        checkBoxes.add(item4);

        radios = new ArrayList<SelectItem>();
        SelectItem item5 = new SelectItem(“Car”, “Car”);
        SelectItem item6 = new SelectItem(“Plane”, “Plane”);
        SelectItem item7 = new SelectItem(“Ship”, “Ship”);
        radios.add(item5);
        radios.add(item6);
        radios.add(item7);

}

    /**
     * @return the checkboxApprovalText
     */
public String getCheckboxApprovalText() {
return checkboxApprovalText;
}

/**
     * @param checkboxApprovalText the checkboxApprovalText to set
     */
public void setCheckboxApprovalText(String checkboxApprovalText) {
this.checkboxApprovalText = checkboxApprovalText;
}

    /**
     * @return the checkBoxes
     */
public List<SelectItem> getCheckBoxes() {
return checkBoxes;
}

   /**
     * @return the renderOutput
     */
public boolean isRenderOutput() {
return renderOutput;
}

    /**
     * @param renderOutput the renderOutput to set
     */
public void setRenderOutput(boolean renderOutput) {
this.renderOutput = renderOutput;
}

  /**
     * @param checkBoxes the checkBoxes to set
     */
public void setCheckBoxes(List<SelectItem> checkBoxes) {
this.setCheckBoxes(checkBoxes);
}

    /**
     * @return the radios
     */
public List<SelectItem> getRadios() {
return radios;
}

    /**
     * @param radios the radios to set
     */
public void setRadios(List<SelectItem> radios) {
this.radios = radios;
}

    /**
     * @return the selectedItemValues
     */
public String getSelectedItemValuesManyCheckBox() {
return selectedItemValuesManyCheckBox;
}

   /**
     * @param selectedItemValues the selectedItemValues to set
     */
public void setSelectedItemValuesManyCheckBox(String selectedItemValues) {
this.selectedItemValuesManyCheckBox = selectedItemValues;
}

/**
* @return the selectedItemValuesSelectOneRadio
*/
public String getSelectedItemValueSelectOneRadio() {
return selectedItemValueSelectOneRadio;
}

    /**
     * @param selectedItemValuesSelectOneRadio the selectedItemValuesSelectOneRadio to set
     */
public void setSelectedItemValueSelectOneRadio(String selectedItemValueSelectOneRadio) {
this.selectedItemValueSelectOneRadio = selectedItemValueSelectOneRadio;
}

    /**
     * @param selectedItemsManyCheckBox the selectedItemsManyCheckBox to set
     */
public void setSelectedItemsManyCheckBox(List<String> selectedItemsManyCheckBox) {
this.selectedItemsManyCheckBox = selectedItemsManyCheckBox;
}

    /**
     * @return the selectedValueBooleanCheckbox
     */
public boolean isSelectedValueBooleanCheckbox() {
return selectedValueBooleanCheckbox;
}

/**
     * @param selectedValueBooleanCheckbox the selectedValueBooleanCheckbox to set
     */
public void setSelectedValueBooleanCheckbox(boolean selectedValueBooleanCheckbox) {
this.selectedValueBooleanCheckbox = selectedValueBooleanCheckbox;
}

public void calledByHtmlJSFCommanButton() {
this.renderOutput = true;
}

    /**
     * @return the selectedItemsManyCheckBox
     */
    public List<String> getSelectedItemsManyCheckBox() {

        if(selectedItemsManyCheckBox !=null) {
        for (String selectedItem : selectedItemsManyCheckBox) {
            selectedItemValuesManyCheckBox += selectedItem + ” “;
        }
        }
        return selectedItemsManyCheckBox;
    }
}

Example output;

Download Source

Advertisements

3 thoughts on “How to use h:selectBooleanCheckbox, h:selectManyCheckbox, h:selectOneRadio JSF 2.0 example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s