JSF – 21 – JSF HTML Tag – 05 – Selection Tags – 01 – Checkbox

Merhaba Arkadaslar.
JSF Selection Tag olarak su componentlere sahiptir ;

  • h:selectBooleanCheckbox
  • h:selectManyCheckbox
  • h:selectOneRadio
  • h:selectOneListbox
  • h:selectManyListbox
  • h:selectOneMenu
  • h:selectManyMenu

Burada su componentleri inceleyecegiz.

  • h:selectBooleanCheckbox
  • h:selectManyCheckbox

h:selectBooleanCheckbox
h:selectBooleanCheckbox tag’i single/tekil bir HTML input checkbox olusturur ve bu checbox’i boolean bean property’e baglayabiliriz (wire).

CheckboxBean.java

package _25.checkbox;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class CheckboxBean {

	private boolean contactMe;

	public boolean isContactMe() {
		return contactMe;
	}

	public void setContactMe(boolean contactMe) {
		this.contactMe = contactMe;
	}
}

checkbox.xhtml

h:selectBooleanCheckbox tag’i value attribute almaktadir.

....
   <h:selectBooleanCheckbox value="#{checkboxBean.contactMe}"/>
...

h:selectBooleanCheckbox tag’i HTML input type checkbox element’ine donusur.

<input type="checkbox" name="j_idt6">

h:selectManyCheckbox
h:selectManyCheckbox tag’ini kullanarak birden fazla checkbox olusturabiliriz. Bunun icin birden fazla yontem kullanabiliriz.

f:selectItem’i kullanarak hardcoded olarak checkBox olusturabiliriz. value attribute’te String[] property belirtiyoruz boylelikle sectigimiz checkboxlarin value’leri bu diziye atanacaktir.

<h2>f:selectItem</h2>
<!-- f:selectItem'i kullanarak hardcoded olarak checkBox olusturabiliriz. -->
<h:selectManyCheckbox value="#{checkboxBean.progLanguages}">
	<f:selectItem itemValue="Java" itemLabel="Java Prog-Lang" />
	<f:selectItem itemValue="C" itemLabel="C Prog-Lang" />
	<f:selectItem itemValue="C++" itemLabel="C++ Prog-Lang" />
	<f:selectItem itemValue="C#" itemLabel="C# Prog-Lang" />
</h:selectManyCheckbox>	

Checkbox.java

....
	// f:selectItem hardcoded
	private String[] progLanguages;

	public String[] getProgLanguages() {
		return progLanguages;
	}

	public void setProgLanguages(String[] progLanguages) {
		this.progLanguages = progLanguages;
	}
...

Bir diger yontem olarak f:selectItems tag’ini kullanabiliriz. f:selectItems’i kullanarak value attribute’te bir String[] array kullanabiliriz.

<h2>f:selectItems with array</h2>

<h:selectManyCheckbox value="#{checkboxBean.jpaFrameworks}">
	<f:selectItems value="#{checkboxBean.preparedJpaFrameworks}"/>
</h:selectManyCheckbox>

#{checkboxBean.prepareJpaFrameworks} getPrepareJpaFrameworks metodunu calistiracaktir.Getter metodunu cagiracak sekilde tanimlamamiz gereklidir ! Secilen checkbox degerleri jpaFrameworks array’ine baglanacaktir(wire)

Checkbox.java

...
        // f:selectItems with array
	private String[] jpaFrameworks;

	public String[] getJpaFrameworks() {
		return jpaFrameworks;
	}

	public void setJpaFrameworks(String[] jpaFrameworks) {
		this.jpaFrameworks = jpaFrameworks;
	}

	public String[] getPreparedJpaFrameworks() {
		return new String[]{"EclipseLink", "Hibernate", "OpenJPA", "DataNucleus"};
	}
...

f:selectItems tag’inda bir array kullanabildigimiz gibi map de kullanabiliriz. Map’teki key checkbox icin text , value ise checkbox icin value attribute’e karsilik gelecektir.

<h2>f:selectItems with map</h2>
<h:selectManyCheckbox value="#{checkboxBean.mvcFrameworks}">
	<f:selectItems value="#{checkboxBean.preparedMVCFrameworks}"/>
</h:selectManyCheckbox>

Checkbox.java

...
	// f:selectItems with map
	private String[] mvcFrameworks;

	public String[] getMvcFrameworks() {
		return mvcFrameworks;
	}
	public void setMvcFrameworks(String[] mvcFrameworks) {
		this.mvcFrameworks = mvcFrameworks;
	}

	public Map<String, String> getPreparedMVCFrameworks() {
		Map<String, String> mvcFrameworksMap = new LinkedHashMap<String, String>();
		mvcFrameworksMap.put("JSF Framework", "JSF");
		mvcFrameworksMap.put("Spring Framework", "Spring");
		mvcFrameworksMap.put("Vaadin Framework", "Vaadin");
		return mvcFrameworksMap;
	}
....

Son olarak f:selectItem Object dizisi kullanabiliriz. Oncelikle label ve value karsilik gelecek bir model olusturalim.

CheckboxModel.java

package _25.checkbox;

public class CheckboxModel {

	private String checkboxLabel;
	private String checkboxValue;
	
	public CheckboxModel(String checkboxLabel, String checkboxValue) {
		super();
		this.checkboxLabel = checkboxLabel;
		this.checkboxValue = checkboxValue;
	}
	public String getCheckboxLabel() {
		return checkboxLabel;
	}
	public void setCheckboxLabel(String checkboxLabel) {
		this.checkboxLabel = checkboxLabel;
	}
	public String getCheckboxValue() {
		return checkboxValue;
	}
	public void setCheckboxValue(String checkboxValue) {
		this.checkboxValue = checkboxValue;
	}
}

f:selectItems tag’inda value olarak kullandigimiz attribute geriye CheckboxModel[] array’i dondurmektedir. itemLabel ve itemValue attribute’leri kullanarak checkbox icin label ve value degerlerini ayarlariz.Bunun icin CheckboxModel sinifinda yer alan property alanlarini kullanabiliriz.

...
<h2>f:selectItems with Object</h2>
   <h:selectManyCheckbox value="#{checkboxBean.databases}">
   <f:selectItems value="#{checkboxBean.preparedDatabases}" var="db"
   itemLabel="#{db.checkboxLabel}" itemValue="#{db.checkboxValue}"
   />
</h:selectManyCheckbox>
...

Checkbox.java

...
	private String[] databases;

	public String[] getDatabases() {
		return databases;
	}

	public void setDatabases(String[] databases) {
		this.databases = databases;
	}

	public CheckboxModel[] getPreparedDatabases() {
		CheckboxModel[] databasesCheckBoxModel = new CheckboxModel[3];
		databasesCheckBoxModel[0] = new CheckboxModel("Oracle", "Oracle RDBMS");
		databasesCheckBoxModel[1] = new CheckboxModel("MSSQL", "MSSQL RDBMS");
		databasesCheckBoxModel[2] = new CheckboxModel("MySQL", "MySQL RDBMS");
		return databasesCheckBoxModel;
	}
...

Sonuclari yazdiracagimiz result.xhtml sayfamiz ;

result.xhtml

<!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:c="http://java.sun.com/jstl/core">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>results</title>
</h:head>
<h:body>
	
	<h2>h:selectBooleanCheckbox</h2>
	
	#{checkboxBean.contactMe}	

	<h2>f:selectItem</h2>
	 <c:forEach var="lang" items="#{checkboxBean.progLanguages}">     
       <p>${lang}</p>
    </c:forEach>
    
    <h2>f:selectItems with array</h2>
	 <c:forEach var="jpa" items="#{checkboxBean.jpaFrameworks}">     
       <p>${jpa}</p>
    </c:forEach>
    
    <h2>f:selectItems with map</h2>
	 <c:forEach var="mvc" items="#{checkboxBean.mvcFrameworks}">     
       <p>${mvc}</p>
    </c:forEach>
    
        
    <h2>f:selectItems with Object</h2>
	 <c:forEach var="database" items="#{checkboxBean.databases}">     
       <p>${database}</p>
    </c:forEach>
	
</h:body>
</html>

CheckboxBean.java

package _25.checkbox;

import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;

@ManagedBean
public class CheckboxBean {

	private boolean contactMe;

	public boolean isContactMe() {
		return contactMe;
	}

	public void setContactMe(boolean contactMe) {
		this.contactMe = contactMe;
	}

	// f:selectItem hardcoded
	private String[] progLanguages;

	public String[] getProgLanguages() {
		return progLanguages;
	}

	public void setProgLanguages(String[] progLanguages) {
		this.progLanguages = progLanguages;
	}

	// f:selectItems with array
	private String[] jpaFrameworks;

	public String[] getJpaFrameworks() {
		return jpaFrameworks;
	}

	public void setJpaFrameworks(String[] jpaFrameworks) {
		this.jpaFrameworks = jpaFrameworks;
	}

	public String[] getPreparedJpaFrameworks() {
		return new String[]{"EclipseLink", "Hibernate", "OpenJPA", "DataNucleus"};
	}

	// f:selectItems with map
	private String[] mvcFrameworks;

	public String[] getMvcFrameworks() {
		return mvcFrameworks;
	}
	public void setMvcFrameworks(String[] mvcFrameworks) {
		this.mvcFrameworks = mvcFrameworks;
	}

	public Map<String, String> getPreparedMVCFrameworks() {
		Map<String, String> mvcFrameworksMap = new LinkedHashMap<String, String>();
		mvcFrameworksMap.put("JSF Framework", "JSF");
		mvcFrameworksMap.put("Spring Framework", "Spring");
		mvcFrameworksMap.put("Vaadin Framework", "Vaadin");
		return mvcFrameworksMap;
	}

	// f:selectItem Object dizisi kullanabiliriz. Oncelikle label ve value
	// karsilik gelecek bir model olusturalim.
	private String[] databases;

	public String[] getDatabases() {
		return databases;
	}

	public void setDatabases(String[] databases) {
		this.databases = databases;
	}

	public CheckboxModel[] getPreparedDatabases() {
		CheckboxModel[] databasesCheckBoxModel = new CheckboxModel[3];
		databasesCheckBoxModel[0] = new CheckboxModel("Oracle", "Oracle RDBMS");
		databasesCheckBoxModel[1] = new CheckboxModel("MSSQL", "MSSQL RDBMS");
		databasesCheckBoxModel[2] = new CheckboxModel("MySQL", "MySQL RDBMS");
		return databasesCheckBoxModel;
	}

}

checkbox.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>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>checkbox components</title>
</h:head>

<h:body>
	<h:form prependId="false" id="formId1">

		<h1>h:selectBooleanCheckbox examples</h1>
	
		<h:selectBooleanCheckbox value="#{checkboxBean.contactMe}"/>
		
		
		<h1>h:selectManyCheckbox examples</h1>
		
		<h2>f:selectItem</h2>
		<!-- f:selectItem'i kullanarak hardcoded olarak checkBox olusturabiliriz. -->
		<h:selectManyCheckbox value="#{checkboxBean.progLanguages}">
   			<f:selectItem itemValue="Java" itemLabel="Java Prog-Lang" />
   			<f:selectItem itemValue="C" itemLabel="C Prog-Lang" />
   			<f:selectItem itemValue="C++" itemLabel="C++ Prog-Lang" />
   			<f:selectItem itemValue="C#" itemLabel="C# Prog-Lang" />
   		</h:selectManyCheckbox>
   		
   		<h2>f:selectItems with array</h2>
   		<!-- f:selectItems'i kullanarak value attribute'te bir String[] array kullanabiliriz.
   		#{checkboxBean.prepareJpaFrameworks} getPrepareJpaFrameworks metodunu calistiracaktir.
   		Getter metodunu cagiracak sekilde tanimlamamiz gereklidir !
   		Secilen checkbox degerleri jpaFrameworks array'ine baglanacaktir(wire)
   		-->
   		<h:selectManyCheckbox value="#{checkboxBean.jpaFrameworks}">
   			<f:selectItems value="#{checkboxBean.preparedJpaFrameworks}"/>
   		</h:selectManyCheckbox>
   		
   		<!-- f:selectItems da array kullanabildigimiz gibi map de kullanabiliriz. -->
   		<h2>f:selectItems with map</h2>
   		<h:selectManyCheckbox value="#{checkboxBean.mvcFrameworks}">
   			<f:selectItems value="#{checkboxBean.preparedMVCFrameworks}"/>
   		</h:selectManyCheckbox>
   		
   		
   		<h2>f:selectItems with Object</h2>
   		<!-- f:selectItem Object dizisi kullanabiliriz.  -->
   		<h:selectManyCheckbox value="#{checkboxBean.databases}">
   			<f:selectItems value="#{checkboxBean.preparedDatabases}" var="db"
   			itemLabel="#{db.checkboxLabel}" itemValue="#{db.checkboxValue}"
   			/>
   		</h:selectManyCheckbox>
   		
   		
   		<p>
			<h:commandButton value="result.xhtml" action="result" />
		</p>
   		
	</h:form>
</h:body>
</html>

Ornegimizi calistiralim , diledigimiz gibi checkboxlari secelim.

h:selectManyCheckbox

Sonuclari gorelim ;

h:selectManyCheckbox result

Checkboxlar icin default secili olmasi icin su sekilde yapabiliriz ;

...
// default secili olarak yapmak icin ;
private boolean contactMe=true;
...
private String[] jpaFrameworks= {"Hibernate"};
..

Kaynak kodlar : Injavawetrust.jsf
Yazimi burada sonlandiriyorum.
Herkese bol Javali gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *