JSF - h:selectManyCheckbox

The h:selectManyCheckbox tag renders a set of HTML input element of type "checkbox", and format it with HTML table and label tags.
<h:selectManyCheckbox value = "#{userData.data}"> 
   <f:selectItem itemValue = "1" itemLabel = "Item 1" /> 
   <f:selectItem itemValue = "2" itemLabel = "Item 2" /> 
</h:selectManyCheckbox>
<table>
   <tr>
      <td>
         <input name = "j_idt6:j_idt8" id = "j_idt6:j_idt8:0" value = "1" 
            type = "checkbox" checked = "checked" />
         <label for = "j_idt6:j_idt8:0" class = ""> Item 1</label>
      </td>
      
      <td>
         <input name = "j_idt6:j_idt8" id = "j_idt6:j_idt8:1" value = "2" 
            type = "checkbox" checked = "checked" />
         <label for = "j_idt6:j_idt8:1" class = ""> Item 2</label>
      </td>     
   </tr>
</table>
id
Identifier for a component
binding
Reference to the component that can be used in a backing bean
rendered
A boolean; false suppresses rendering
styleClass
Cascading stylesheet (CSS) class name
value
A component’s value, typically a value binding
valueChangeListener
A method binding to a method that responds to value changes
converter
Converter class name
validator
Class name of a validator that’s created and attached to a component
required
A boolean; if true, requires a value to be entered in the associated field
accesskey
A key, typically combined with a system-defined metakey, that gives focus to an element
accept
Comma-separated list of content types for a form
accept-charset
Comma- or space-separated list of character encodings for a form. The accept-charset attribute is specified with the JSF HTML attribute named acceptcharset.
alt
Alternative text for nontextual elements such as images or applets
charset
Character encoding for a linked resource
coords
Coordinates for an element whose shape is a rectangle, circle, or polygon
dir
Direction for text. Valid values are ltr (left to right) and rtl (right to left)
disabled
Disabled state of an input element or button
hreflang
Base language of a resource specified with the href attribute; hreflang may only be used with href.
lang
Base language of an element’s attributes and text
maxlength
Maximum number of characters for text fields
readonly
Read-only state of an input field; text can be selected in a readonly field but not edited
rel
Relationship between the current document and a link specified with the href attribute
rev
Reverse link from the anchor specified with href to the current document. The value of the attribute is a space-separated list of link types
rows
Number of visible rows in a text area. h:dataTable has a rows attribute, but it’s not an HTML pass-through attribute.
shape
Shape of a region. Valid values: default, rect, circle, poly. (default signifies the entire region)
style
Inline style information
tabindex
Numerical value specifying a tab index
target
The name of a frame in which a document is opened
title
A title, used for accessibility, that describes an element. Visual browsers typically create tooltips for the title’s value
type
Type of a link; for example, stylesheet
width
Width of an element
onblur
Element loses focus
onchange
Element’s value changes
onclick
Mouse button is clicked over the element
ondblclick
Mouse button is double-clicked over the element
onfocus
Element receives focus
onkeydown
Key is pressed
onkeypress
Key is pressed and subsequently released
onkeyup
Key is released
onmousedown
Mouse button is pressed over the element
onmousemove
Mouse moves over the element
onmouseout
Mouse leaves the element’s area
onmouseover
Mouse moves onto an element
onmouseup
Mouse button is released
onreset
Form is reset
onselect
Text is selected in an input field
disabledClass
CSS class for disabled elements
enabledClass
CSS class for enabled elements
layout
Specification for how elements are laid out: lineDirection (horizontal) or pageDirection (vertical)
border
Border of the element
Let us create a test JSF application to test the above tag.
package com.tutorialspoint.test;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
   private static final long serialVersionUID = 1L;
   public String[] data = {"1","2","3"};
   
   public String[] getData() {
      return data;
   }

   public void setData(String[] data) {
      this.data = data;
   }
}
<?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">

<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/jsf/html">
   
   <head>
      <title>JSF Tutorial!</title>
   </head>
   
   <h:body>
      <h2>h:selectManyCheckbox example</h2>
      <hr />
      
      <h:form>
         <h3>Mutiple checkboxes</h3> 
         <h:selectManyCheckbox value = "#{userData.data}">
            <f:selectItem itemValue = "1" itemLabel = "Item 1" />
            <f:selectItem itemValue = "2" itemLabel = "Item 2" />
            <f:selectItem itemValue = "3" itemLabel = "Item 3" />
            <f:selectItem itemValue = "4" itemLabel = "Item 4" />
            <f:selectItem itemValue = "5" itemLabel = "Item 5" />
         </h:selectManyCheckbox>
         <h:commandButton value = "Submit" action = "result" />
      </h:form> 		
   
   </h:body>
</html> 
<?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">

<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/jsf/html"
   xmlns:ui = "http://java.sun.com/jsf/facelets">
   
   <h:body>
      <h2>Result</h2>
      <hr />
      
      <ui:repeat value = "#{userData.data}" var = "s">		
         #{s}
      </ui:repeat>   	
   </h:body>
</html> 
Once you are ready with all the changes done, let us compile and run the application as we did in JSF - Create Application chapter. If everything is fine with your application, this will produce the following result.
Select multiple checkboxes and press Submit button. We've selected four items. You will see the selected results.

    37 Lectures 
     3.5 hours 

 Chaand Sheikh
 Print
 Add Notes
 Bookmark this page