JSF 2 SelectBooleanCheckbox Example

The selectBooleanCheckbox component is one of the main component that provided in the JSF 1 and included in the JSF 2. The selectBooleanCheckbox simply renders a checkbox that can be wired to a boolean bean property and corresponding representation of it by means of HTML is <input type=”checkbox”/>. The main attribute of that component is a value that could be associated (binded) to a boolean property.

JSF 2 provides additional attributes for selectBooleanCheckbox, those added attributes are mainly related to the styles and the way that the data is displayed within it; enabledClass, disabledClass, selectedClass, unSelectedClass, hideNoSelectionOption and collectionType are the main newly attributes that added since JSF 2 has took place.

Also Read:

1. Managed Bean

SelectBooleanCheckboxBean.java

package net.javabeat.jsf;

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

@ManagedBean
@SessionScoped
public class SelectBooleanCheckboxBean {

	private boolean trainee;

	public SelectBooleanCheckboxBean(){

	}

	public String register(){
		return "register";
	}

	public boolean isTrainee() {
		return trainee;
	}

	public void setTrainee(boolean trainee) {
		this.trainee = trainee;
	}
}

2. The Views

Next coming scenario shows you a sample view that displays a portion of sample training registration form,that form provides the user a boolean indicator input for determining the type of the participant whether is a trainee or not.

selectBooleanCheckbox.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
	<h:form>
		<h1><h:outputText value="JavaBeat JSF 2.2 Examples" /></h1>
		<h2><h:outputText value="SelectBooleanCheckbox Example" /></h2>
		<h:outputText value="Is Trainee"/>
		<h:selectBooleanCheckbox value="#{selectBooleanCheckboxBean.trainee}"></h:selectBooleanCheckbox>
		<h:commandButton value="Register" action="#{selectBooleanCheckboxBean.register}"></h:commandButton>
	</h:form>
</html>

register.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
	<f:view>
		<h1><h:outputText value="JavaBeat JSF 2.2 Examples" /></h1>
		<h2><h:outputText value="SelectBooleanCheckbox Example" /></h2>
		<h:form>
			<h:outputText value="Is Trainee: "/>
			<h:outputText value="#{selectBooleanCheckboxBean.trainee}"/>
		</h:form>
	</f:view>
</html>

3. The Deployment Descriptor

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

	id="WebApp_ID" version="2.5" metadata-complete="true">
	<context-param>
		<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
		<param-value>client</param-value>
	</context-param>
	<context-param>
		<param-name>javax.faces.application.CONFIG_FILES</param-name>
		<param-value>/WEB-INF/faces-config.xml</param-value>
	</context-param>
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>/faces/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>
	<listener>
		<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
	</listener>
</web-app>

4. JSF 2 SelectBooleanCheckBox Demo

The below snapshots show you the complete scenario for using the SelectBooleanCheckbox.
JSF 2 SelectBooleanCheckbox Example 1

JSF 2 SelectBooleanCheckbox Example 2

Comments

comments

About Amr Mohammed

Speak Your Mind

*