JSF – 23 – JSF HTML Tag – 07 – Selection Tags – 03 – Dropdown Menu

Merhaba Arkadaslar.
Bu bolumde dropdown menu componentlerini inceleyecegiz. Bu component’ler ;

  • h:selectOneMenu
  • h:selectManyMenu

h:selectOneMenu
h:selectOneMenu tag’i HTML select elementine donusturulur.

<h1>h:selectOneMenu examples</h1>

<h2>f:selectItem</h2>
<h:selectOneMenu value="#{dropdownBean.language}">
	<f:selectItem itemValue="English" itemLabel="en" />
	<f:selectItem itemValue="Turkish" itemLabel="tr" />
	<f:selectItem itemValue="Italian" itemLabel="it" />
</h:selectOneMenu>
<select name="j_idt6" size="1"> 
  <option value="English">en</option>
  <option value="Turkish">tr</option>
  <option value="Italian">it</option>
</select>

DropdownBean.java

package _27.dropdown;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class DropdownBean {

	private String language;

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
...
}

Onceki bolumlerde f:selectItems in array , map ve object array’lerle kullanimini incelemistik. Burada sadece String[] array kullanimini inceleyelim.

f:selectItems tag’inda getPreparedYears metodunu kullanalim ve secilen degeri year degiskenine atayalim.

 <h2>f:selectItems</h2>
 <h:selectOneMenu value="#{dropdownBean.year}">
      <f:selectItems value="#{dropdownBean.preparedYears}"/>
 </h:selectOneMenu>
	private String year;

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String[] getPreparedYears() {
		return new String[]{"2015", "2016", "2017", "2018"};
	}

Ornegimizi calistirdigimizda ;

h:selectOneMenu

dropdown menuden secim yapip butona tikladigimizda ;

h:selectOneMenu result

h:selectManyMenu
h:selectManyMenu , HTML select multiple option elementine donusturulur.

 .... 
 <h2>f:selectItem</h2>
 <h:selectManyMenu value="#{multipleDropdownBean.languages}">
     <f:selectItem itemValue="English" itemLabel="en" />
     <f:selectItem itemValue="Turkish" itemLabel="tr" />
     <f:selectItem itemValue="Italian" itemLabel="it" />
 </h:selectManyMenu>
....
 <select name="j_idt6" multiple="multiple" size="5"> 
     <option value="English">en</option>
     <option value="Turkish">tr</option>
     <option value="Italian">it</option>
</select>

MultipleDropdownBean.java

package _28.multiple.dropdown;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class MultipleDropdownBean {

	private String[] languages;
	
	public String[] getLanguages() {
		return languages;
	}
...
}

Onceki orneklerde yaptigimiz gibi f:selectItems tag’ini kullanabiliriz. Select-option elementi icin preparedYears metodunu kullaniyoruz , bu metot geriye String[] array dondurmektedir. Birden fazla secim yapabilecegimiz icin years String[] dizisini kullaniyoruz.

 <h2>f:selectItems</h2>
     <h:selectManyMenu value="#{multipleDropdownBean.years}">
     <f:selectItems value="#{multipleDropdownBean.preparedYears}"/> 
 </h:selectManyMenu>
...

	private String[] years;

	public String[] getYears() {
		return years;
	}

	public void setYears(String[] years) {
		this.years = years;
	}

	public String[] getPreparedYears() {
		return new String[]{"2015", "2016", "2017", "2018"};
	}
...

Ornegimizi calistiralim ;

h:selectManyMenu

Birden fazla secim yaparak butona tikladigimizda ;

h:selectManyMenu results

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 *