JSF列表框


以下部分介紹如何在JSF中建立ListBox(JSF表列表框)。<h:selectOneListbox>標籤呈現指定大小的「select」型別的HTML輸入元素。

以下JSF程式碼 -

<h:selectOneListbox value="#{userData.data}">
   <f:selectItem itemValue="1" itemLabel="Item 1" />
   <f:selectItem itemValue="2" itemLabel="Item 2" />                   
</h:selectOneListbox>

被渲染成以下HTML標籤-

<select name="j_idt6:j_idt8" size="2">  
   <option value="1">Item 1</option>
   <option value="2">Item 2</option>
</select>

寫死範例表列表框

以下是檔案:UserBean.java 中的程式碼 -

package com.tw511.common;

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 item;

  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }    
}

以下是檔案:index.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:body>
      <h:form>
        Hard-coded with "f:selectItem" : 
       <h:selectOneListbox value="#{user.item}">
         <f:selectItem itemValue="1" itemLabel="item 1" />
         <f:selectItem itemValue="2" itemLabel="item 2" />
         <f:selectItem itemValue="3" itemLabel="item 3" />
       </h:selectOneListbox>

    <br /><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>
  <p>Selected: #{user.item}</p>
</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>
  <p>Selected: #{user.item}</p>
</h:body>
</html>

以下是檔案:UserBean.java 中的程式碼 -

package com.yiibai;

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 item;

  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }  
  //Generated by Map
  private static Map<String,Object> itemValue;
  static{
    itemValue = new LinkedHashMap<String,Object>();
    itemValue.put("Item 1", "1"); //label, value
    itemValue.put("Item 2", "2");
    itemValue.put("Item 3", "3");
  }  
  public Map<String,Object> getItemValue() {
    return itemValue;
  }    
}

以下是檔案:index.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:body>
      <h:form>
        Generated by Map :
       <h:selectOneListbox value="#{user.item}">
         <f:selectItems value="#{user.itemValue}" />
       </h:selectOneListbox>

    <br /><br />
      <h:commandButton value="Submit" action="result" />
    <h:commandButton value="Reset" type="reset" />
      </h:form>
    </h:body>
</html>

內部類istBox實現列表框

以下是檔案:UserBean.java 中的程式碼 -

package com.yiibai;

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 item;

  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }  
  public static class Item{
    public String label;
    public String value;

    public Item(String l, String v){
      this.label = l;
      this.value = v;
    }

    public String getLabel(){
      return label;
    }

    public String getValue(){
      return value;
    }

  }
  public Item[] itemList;

  public Item[] getItemValue() {
    itemList = new Item[3];
    itemList[0] = new Item("item - 1", "1");
    itemList[1] = new Item("item - 2", "2");
    itemList[2] = new Item("item - 3", "3");

    return itemList;
  }    
}

以下是檔案: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>
  <p>Selected: #{user.item}</p>
</h:body>
</html>

以下是檔案:index.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:body>
      <h:form>
        Generated by Object array and iterate with var :
       <h:selectOneListbox value="#{user.item}">
         <f:selectItems value="#{user.itemValue}" var="y"
         itemLabel="#{y.label}" itemValue="#{y.value}" />
       </h:selectOneListbox>

    <br /><br />
      <h:commandButton value="Submit" action="result" />
    <h:commandButton value="Reset" type="reset" />
      </h:form>
    </h:body>
</html>