JSF – 24 – JSF HTML Tag – 08 – Selection Tags – 04 – Listboxes

Merhaba Arkadaslar
Bu bolumde Listboxes componentlerini inceleyecegiz. Bu componentler;

  • h:selectOneListbox
  • h:selectManyListbox

h:selectOneListbox
h:selectOneListbox component’i HTML select elementine donusturulur. h:selectOneMenu den farkli olarak HTML select elementine donusturulurken size attribute eklenir.

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

OneListboxBean.java

package _29.onelistbox;

import java.util.Arrays;
import java.util.List;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class OneListboxBean {

	private String language;

	public String getLanguage() {
		return language;
	}

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

f:selectItems tag’i ile array , map ve object array kullanabiliriz. Benzer sekilde List de kullanabiliriz.

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

 public String getYear() {
 return year;
 }

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

 public List<String> getPreparedYears() {
 return Arrays.asList(new String[]{"2015", "2016", "2017", "2018"});
 }

result.xhtml

.... 
 <h1>h:selectOneListbox results</h1>

 <h2>f:selectItem</h2>
 #{oneListboxBean.language}
 
 <h2>f:selectItems</h2>
 #{oneListboxBean.year} 
....

Ornegimizi calistirdigimizda ;

h:selectOneListbox

h:selectOneListbox result

h:selectManyListbox
h:selectManyListbox , HTML select elementine donusturulur , multiple ve size attribute eklenir.

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

MultipleListboxBean.java

package _30.multiple.listbox;

import java.util.Arrays;
import java.util.List;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class MultipleListboxBean {

	private String[] languages;

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

	public void setLanguages(String[] languages) {
		this.languages = languages;
	}
...
}

f:selectItems ve List<String> kullanalim ;

 <h2>f:selectItems</h2>
 <h:selectManyListbox value="#{multipleListboxBean.years}">
     <f:selectItems value="#{multipleDropdownBean.preparedYears}"/> 
 </h:selectManyListbox>
	private String[] years;

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

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

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

Ornegimizi calistiralim ;

h:selectManyListbox

h:selectManyListbox result

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 *