Table of contents
1.
Introduction
2.
JSF
3.
h:selectManyCheckbox
4.
JSF tag
5.
Rendered Output
6.
Attributes of h:selectManyCheckbox
7.
Example
7.1.
test.xhtml
7.2.
faces-config.xml 
7.3.
web.xml
7.4.
Welcome.xhtml
7.5.
Output
8.
Frequently Asked Questions
8.1.
What is JSF (JavaServer Faces), and how does it work?
8.2.
What exactly is an eagerly managed bean?
8.3.
What does the Controller module do?
8.4.
In JSF (JavaServer Faces), what is a managed bean?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

JSF - h:selectManyCheckbox

Author Ayush Mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you ever been in a situation where you have to select multiple statements? While developing websites, you will encounter problems requiring you to select more than one option. JSF provides a built-in tag to choose various options. In this blog, you will learn one such tag, h:selectManyCheckbox. Let's get started!!

JSF

JSF (Java Server Faces) is a framework for designing user interfaces on the server-side. It's developed in Java, has a component-based architecture, and provides standard management tools for these components. It has a lot of sophisticated APIs and a lot of tag libraries.

The APIs are responsible for providing components and managing their state. Server-side validation, extensibility, accessibility, and data transformations are all made easier. Tag libraries make it easier to add components on web pages and relate them to backend objects. It has handles for applying component tags.

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

  1. Id: tag’s id
  2. rendered: The value is a boolean; false disables rendering.
  3. value: binding of a value.
  4. validator: validator connected to the component's class name.
  5. valueChangeListener: A method binding that reacts to changes in value.
  6. accept-charset: A collection of character encodings for a form separated by commas or spaces. 
  7. border: The width of an element's border in pixels.
  8. coords: Coordinates for a rectangle, circle, or polygon-shaped element.
  9. hreflang: A base language of a resource specified with the href attribute; 
  10. maxlength: Text fields are limited to a certain number of characters.
  11. dir: The text's direction ltr (left to right) and rtl (right to the left) are both acceptable options (right to the left).
  12. title: A title that is used to improve accessibility. Browsers usually create tooltips for the title's value.
  13. target: The name of the window in which a document is shown when it is first opened.
  14. ondblclick: Double-clicked mouse button event handler
  15. onkeyup: Event handler for Key released
  16. onkeydown: Event handler for Key pressed
  17. onkeypress: Event handler for Key pressed and released
  18. onmousedown: Event handler for Mouse button pressed
  19. onmousemove: Event handler for mouse moved
  20. onmouseout: Event handler for mouse left
  21. onreset: Event handler for form reset
  22. onselect: Event handler for text selected
  23. immediate: Validation of processes occurs early in the life cycle.
  24. type: Stylesheet, for instance, is one type of link.
  25. disabled: The status of an input element or a button when it is disabled.
  26. binding: A backing bean's component is referred to as a backing bean.
  27. readonly: An input field's read-only status
  28. charset: A connected resource's character encoding
  29. accesskey: focuses on a specific element
  30. 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!

Live masterclass