JSFでは、

<h:selectOneListbox/>

タグが単一の選択リストボックスをレンダリングするために使用されます – HTML

select要素を ”

size ** “属性でレンダリングします。

…​.//JSF…​
<h:selectOneListbox value=”#{user.favYear1}”>
<f:selectItem itemValue=”2000″ itemLabel=”Year1 – 2000″/>
<f:selectItem itemValue=”2010″ itemLabel=”Year1 – 2010″/>
<f:selectItem itemValue=”2020″ itemLabel=”Year1 – 2020″/>
</h:selectOneListbox>
<select name=”j

idt6:j

idt8″ size=”3″>
<option value=”2000″>Year1 – 2000</option>
<option value=”2010″>Year1 – 2010</option>
<option value=”2020″>Year1 – 2020</option>
</select>

===  h:selectOneListboxの例

単一のselectリストボックスをレンダリングするための "**  h:selectOneListbox ** "タグの使用方法を示すJSF 2.0の例を示し、3つの異なる方法でデータを設定します。

.  "**  f:selectItem ** "タグのハードコードされた値.

. マップで値を生成し、それを "**  f:selectItems ** "タグに入れます.

. オブジェクト配列を使って値を生成し、それを "**  f:selectItems ** "に入れる

タグを使用し、値を "**  var ** "属性で表します。

===  1.バッキングビーン

リストボックス値のデータを保持および生成するバッキングBean。

package com.mkyong;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name=”user”)
@SessionScoped
public class UserBean implements Serializable{

public String favYear1;
public String favYear2;
public String favYear3;

//getter and setter methods

//Generated by Map
 private static Map<String,Object> year2Value;
 static{
     year2Value = new LinkedHashMap<String,Object>();
     year2Value.put("Year2 - 2000", "2000");//label, value
     year2Value.put("Year2 - 2010", "2010");
     year2Value.put("Year2 - 2020", "2020");
 }

public Map<String,Object> getFavYear2Value() {
    return year2Value;
}

//Generated by Object array
 public static class Year{
     public String yearLabel;
     public String yearValue;

public Year(String yearLabel, String yearValue){
    this.yearLabel = yearLabel;
    this.yearValue = yearValue;
}

public String getYearLabel(){
    return yearLabel;
}

public String getYearValue(){
    return yearValue;
}

}

public Year[]year3List;

public Year[]getFavYear3Value() {

year3List = new Year[3];
year3List[0]= new Year("Year3 - 2000", "2000");
year3List[1]= new Year("Year3 - 2010", "2010");
year3List[2]= new Year("Year3 - 2020", "2020");

    return year3List;
}

}

===  2. JSFページ

"**  h:selectOneListbox ** "タグの使用方法を示すJSFページ。

<?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:body>

<h1>JSF 2 listbox example</h1>
<h:form>

  1. Hard-coded with “f:selectItem” :
    <h:selectOneListbox value=”#{user.favYear1}”>
    <f:selectItem itemValue=”2000″ itemLabel=”Year1 – 2000″/>
    <f:selectItem itemValue=”2010″ itemLabel=”Year1 – 2010″/>
    <f:selectItem itemValue=”2020″ itemLabel=”Year1 – 2020″/>
    </h:selectOneListbox>

    <br/>

  2. Generated by Map :
    <h:selectOneListbox value=”

    {user.favYear2}”>
    <f:selectItems value=”

    {user.favYear2Value}”/>
    </h:selectOneListbox>

    <br/>

  3. Generated by Object array and iterate with var :
    <h:selectOneListbox value=”

    {user.favYear3}”>
    <f:selectItems value=”

    {user.favYear3Value}” var=”y”
    itemLabel=”

    {y.yearLabel}” itemValue=”

    {y.yearValue}”/>
    </h:selectOneListbox>

    <br/>

        <h:commandButton value="Submit" action="result"/>
    <h:commandButton value="Reset" type="reset"/>

            </h:form>
        </h:body>
    </html>

    
    

result.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"
      >

    <h:body>

        <h1>JSF 2 listbox example</h1>

        <h2>result.xhtml</h2>

        <ol>
            <li>user.favYear1 : #{user.favYear1}</li>
            <li>user.favYear2 : #{user.favYear2}</li>
            <li>user.favYear3 : #{user.favYear3}</li>
        </ol>
    </h:body>

</html>

デモ


jsf2-listbox-example-1、title = "jsf2-listbox-example-1"、width = 640、height = 438

「送信」ボタンをクリックすると、「result.xhtml」ページにリンクし、送信されたリストボックス値を表示します。


jsf2-listbox-example-2、title = "jsf2-listbox-example-2"、width = 640、height = 440

リストボックスの値をあらかじめ選択する方法は?



h:selectOneListbox

“タグの “value”と一致する場合、 ”

f:selectItems

“タグの値が選択されます。上記の例で、favYear1プロパティを “2010”に設定した場合は、次のようになります。

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

    public String favYear1 = "2010";

   //...

「favYear1」リストボックスの値「2010」がデフォルトで選択されています。

ソースコードをダウンロードする

ダウンロード – リンク://wp-content/uploads/2010/10/JSF-2-Listbox-Example.zip[JSF-2-Listbox-Example.zip](10KB)