h:selectManyCheckbox
The h:selectManyCheckbox tag creates a collection of HTML input elements of the type "checkbox" that is formatted with HTML table and label tags.
JSF tag
<h:selectManyCheckbox value = "#{coding.data}">
<f:selectItem itemValue = "1" itemLabel = "Label 1" />
<f:selectItem itemValue = "2" itemLabel = "Label 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 = ""> Label 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 = ""> Label 2</label>
</td>
</tr>
</table>
Attributes of h:selectManyCheckbox
- Id: tag’s id
- rendered: The value is a boolean; false disables rendering.
- value: binding of a value.
- validator: validator connected to the component's class name.
- valueChangeListener: A method binding that reacts to changes in value.
- accept-charset: A collection of character encodings for a form separated by commas or spaces.
- border: The width of an element's border in pixels.
- coords: Coordinates for a rectangle, circle, or polygon-shaped element.
- hreflang: A base language of a resource specified with the href attribute;
- maxlength: Text fields are limited to a certain number of characters.
- dir: The text's direction ltr (left to right) and rtl (right to the left) are both acceptable options (right to the left).
- title: A title that is used to improve accessibility. Browsers usually create tooltips for the title's value.
- target: The name of the window in which a document is shown when it is first opened.
- ondblclick: Double-clicked mouse button event handler
- onkeyup: Event handler for Key released
- onkeydown: Event handler for Key pressed
- onkeypress: Event handler for Key pressed and released
- onmousedown: Event handler for Mouse button pressed
- onmousemove: Event handler for mouse moved
- onmouseout: Event handler for mouse left
- onreset: Event handler for form reset
- onselect: Event handler for text selected
- immediate: Validation of processes occurs early in the life cycle.
- type: Stylesheet, for instance, is one type of link.
- disabled: The status of an input element or a button when it is disabled.
- binding: A backing bean's component is referred to as a backing bean.
- readonly: An input field's read-only status
- charset: A connected resource's character encoding
- accesskey: focuses on a specific element
- tabindex: A tab index is specified by a numerical integer.
Example
This program will help you to understand the h:selectManyCheckbox
User-managed bean code
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="test")
@SessionScoped
public class Test {
private String[] checkBoxValues;
public String[] getCheckBoxValues() {
return checkBoxValues;
}
public void setCheckBoxValues(String[] checkBoxValues) {
this.checkBoxValues = checkBoxValues;
}
public String checkCheckBoxValue() {
return "success";
}
}
test.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF select many check box example.</title>
</h:head>
<h:body>
<h2>JSF select many check box example.</h2>
<h:form>
<h:selectManyCheckbox value="#{test.checkBoxValues}">
<f:selectItem itemValue="MCA" itemLabel="MCA" />
<f:selectItem itemValue="CSE" itemLabel="CSE" />
<f:selectItem itemValue="IT" itemLabel="IT" />
</h:selectManyCheckbox>
<br/> <br/>
<h:commandButton value="Check CheckBox Value"
action="#{test.checkCheckBoxValue}"/>
</h:form>
</h:body>
</html>
faces-config.xml
<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<navigation-rule>
<from-view-id>test.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>welcome.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
Welcome.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>JSF select many check box example.</title>
</h:head>
<h:body>
<h3>Selected CheckBox values: </h3>
<ui:repeat value="#{test.checkBoxValues}"
var="selected">
<h:outputText value="#{selected}"/>
<br/>
</ui:repeat>
</h:body>
</html>
Output



Frequently Asked Questions
What is JSF (JavaServer Faces), and how does it work?
It's a component-based user interface framework that runs on the server. It's a programming language that's used to create web apps. It comes with a well-defined programming model and a robust API and tag library. Facelets is the default templating mechanism in the newest version of JSF 2.
What exactly is an eagerly managed bean?
By default, the Managed bean is lazy. It signifies that the bean is only instantiated when the application makes a request.
What does the Controller module do?
It is in charge of an application's processing.
In JSF (JavaServer Faces), what is a managed bean?
It is a pure Java class with a collection of properties and getter and setter methods.
Conclusion
Finally, you've concluded this article. Congratulations!! You learned about h:selectManyCheckbox in JSF in this blog. You studied the tag, its properties, and a sample program.
After reading these articles, are you eager to read more articles on the subject of JSF? Don't worry; Coding Ninjas will take care of everything. See the JSF for further information.
Please see our Code studio guided routes to learn more about DSA, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.
Happy Learning!