JSF - h:selectManyCheckbox

Advertisements


The h:selectManyCheckbox tag renders a set of HTML input element of type "checkbox", and format it with HTML table and label tags.

JSF Tag

<h:selectManyCheckbox value="#{userData.data}">
   <f:selectItem itemValue="1" itemLabel="Item 1" />
   <f:selectItem itemValue="2" itemLabel="Item 2" />
</h:selectManyCheckbox>

Rendered Output

<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>

Tag Attributes

S.N.Attribute & Description
1id
Identifier for a component
2binding
Reference to the component that can be used in a backing bean
3rendered
A boolean; false suppresses rendering
4styleClass
Cascading stylesheet (CSS) class name
5value
A component’s value, typically a value binding
6valueChangeListener
A method binding to a method that responds to value changes
7converter
Converter class name
8validator
Class name of a validator that’s created and attached to a component
9required
A boolean; if true, requires a value to be entered in the associated field
10accesskey
A key, typically combined with a system-defined metakey, that gives focus to an element
11accept
Comma-separated list of content types for a form
12accept-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.
13alt
Alternative text for nontextual elements such as images or applets
14charset
Character encoding for a linked resource
15coords
Coordinates for an element whose shape is a rectangle, circle, or polygon
16dir
Direction for text. Valid values are ltr (left to right) and rtl (right to left).
17disabled
Disabled state of an input element or button
18hreflang
Base language of a resource specified with the href attribute; hreflang may only be used with href.
19lang
Base language of an element’s attributes and text
20maxlength
Maximum number of characters for text fields
21readonly
Read-only state of an input field; text can be selected in a readonly field but not edited
22rel
Relationship between the current document and a link specified with the href attribute
23rev
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.
24rows
Number of visible rows in a text area. h:dataTable has a rows attribute, but it’s not an HTML pass-through attribute.
25shape
Shape of a region. Valid values: default, rect,circle, poly. (default signifies the entire region)
26style
Inline style information
27tabindex
Numerical value specifying a tab index
28target
The name of a frame in which a document is opened
29title
A title, used for accessibility, that describes an element. Visual browsers typically create tooltips for the title’s value
30type
Type of a link; for example, stylesheet
31width
Width of an element
32onblur
Element loses focus
33onchange
Element’s value changes
34onclick
Mouse button is clicked over the element
35ondblclick
Mouse button is double-clicked over the element
36onfocus
Element receives focus
37onkeydown
Key is pressed
38onkeypress
Key is pressed and subsequently released
39onkeyup
Key is released
40onmousedown
Mouse button is pressed over the element
41onmousemove
Mouse moves over the element
42onmouseout
Mouse leaves the element’s area
43onmouseover
Mouse moves onto an element
44onmouseup
Mouse button is released
45onreset
Form is reset
46onselect
Text is selected in an input field
47disabledClass
CSS class for disabled elements
48enabledClass
CSS class for enabled elements
49layout
Specification for how elements are laid out: lineDirection (horizontal) or pageDirection (vertical)
50border
border of the element

Example Application

Let us create a test JSF application to test the above tag.

StepDescription
1Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
2Modify home.xhtml as explained below. Keep rest of the files unchanged.
3Create result.xhtml in the webapps directory as explained below.
4Create UserData.java as a managed bean under package com.tutorialspoint.test as explained below.
5Compile and run the application to make sure business logic is working as per the requirements.
6Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
7Launch your web application using appropriate URL as explained below in the last step.

UserData.java

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;
   }
}

home.xhtml

<?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> 

result.xhtml

<?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 following result:

JSF h:selectManyCheckbox

Select multiple checkboxes and press Submit button.We've selected four items. You will see the selected results.

JSF h:selectManyCheckbox1

Advertisements
Advertisements