鍍金池/ 教程/ HTML/ JSF表單組合框
JSF數(shù)據(jù)表(h:dataTable)添加刪除
JSF <h:commandLink>標(biāo)簽
JSF應(yīng)用程序入門示例
JSF數(shù)據(jù)表(ui:repeat)創(chuàng)建表
JSF列表框
JSF數(shù)據(jù)表(h:dataTable)DataModel排序數(shù)據(jù)
JSF復(fù)合組件
JSF <h:inputText>標(biāo)簽
JSF表單組合框
JSF <h:messages>標(biāo)簽
JSF <h:message>標(biāo)簽
JSF轉(zhuǎn)換日期時(shí)間
JSF JDBC連接
JSF <h:inputHidden>標(biāo)簽
JSF多選列表框
JSF <h:inputSecret>標(biāo)簽
JSF自定義轉(zhuǎn)換器
JSF <f:ajax>標(biāo)簽
JSF生命周期
JSF可重定位資源
JSFJSF用戶界面組件模型
JSF <h:attribute>標(biāo)簽
JSF驗(yàn)證器標(biāo)簽
JSF驗(yàn)證字符串長度
JSF轉(zhuǎn)換器標(biāo)簽
JSF托管bean(Managed Bean)
JSF值變化的事件
JSF UI組件示例
JSF MySQL CURD實(shí)例
JSF數(shù)據(jù)表(h:dataTable)排序數(shù)據(jù)
JSF <h:graphicImage>標(biāo)簽
JSF <f:convertNumber>標(biāo)簽
JSF教程
JSF托管Bean
JSF輸出腳本
JSF <h:outputText>標(biāo)簽
JSF操作事件
JSF驗(yàn)證正則表達(dá)式
JSF數(shù)據(jù)表(h:dataTable)行號(hào)
JSF <h:setPropertyActionListener>標(biāo)簽
JSF注入托管bean實(shí)例
JSF <h:commandButton>標(biāo)簽
JSF Web資源
JSF <h:inputFile>標(biāo)簽
JSF驗(yàn)證浮點(diǎn)數(shù)值范圍
JSF Facelets視圖
JSF是什么?
JSF Facelets模板
JSF的特性(特點(diǎn))
JSF自定義驗(yàn)證器類
JSF單選按鈕
JSF輸出樣式
JSF數(shù)據(jù)表(h:dataTable)更新數(shù)據(jù)
JSF HTML5友好標(biāo)記
JSF表單復(fù)選框(CheckBox)示例
JSF <h:form>標(biāo)簽
JSF Facelets技術(shù)介紹
JSF輸出格式化
JSF <h:inputtextarea>標(biāo)簽
JSF驗(yàn)證整數(shù)范圍
JSF <h:panelGrid>標(biāo)簽

JSF表單組合框

以下部分介紹如何從JSF創(chuàng)建HTML組合框。<h:selectOneMenu>標(biāo)簽呈現(xiàn)大小未指定的“select”類型的HTML輸入元素。

以下JSF標(biāo)簽 -

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

被渲染成以下HTML代碼 -

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

硬編碼組合框?qū)嵗?/h2>

以下是文件: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:selectOneMenu value="#{user.favItem}">
         <f:selectItem itemValue="A" itemLabel="Coffee A" />
         <f:selectItem itemValue="B" itemLabel="Coffee B" />
         <f:selectItem itemValue="C" itemLabel="Coffee C" />
       </h:selectOneMenu>

    <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>user.favItem : #{user.favItem}</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 favItem = "Black";
  public String getFavItem() {
    return favItem;
  }

  public void setFavItem(String f) {
    this.favItem = f;
  }
}

由映射生成組合框?qū)嵗?/h2>

以下是文件: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 favItem;
  public String getFavItem() {
    return favItem;
  }

  public void setFavItem(String favItem) {
    this.favItem = favItem;
  }
  //Generated by Map
  private static Map<String,Object> value;
  static{
    value = new LinkedHashMap<String,Object>();
    value.put("Item A", "A"); //label, value
    value.put("Item B", "B");
    value.put("Item C", "C");
  }

  public Map<String,Object> getFavItemValue() {
    return value;
  }  
}

以下是文件: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:selectOneMenu value="#{user.favItem}">
         <f:selectItems value="#{user.favItemValue}" />
       </h:selectOneMenu>

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

組合框內(nèi)部項(xiàng)

以下是文件: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:selectOneMenu value="#{user.favItem}">
         <f:selectItems value="#{user.itemValue}" var="c" itemLabel="#{c.itemLabel}" itemValue="#{c.itemValue}" />
       </h:selectOneMenu>
    <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.favItem}</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 favItem;
  public String getFavItem() {
    return favItem;
  }
  public void setFavItem(String f) {
    this.favItem = f;
  }
  //Generated by Object array
  public static class Item{
    public String itemLabel;
    public String itemValue;

    public Item(String l, String v){
      this.itemLabel = l;
      this.itemValue = v;
    }
    public String getItemLabel(){
      return itemLabel;
    }
    public String getItemValue(){
      return itemValue;
    }
  }
  public Item[] itemList;
  public Item[] getItemValue() {
    itemList = new Item[3];
    itemList[0] = new Item("Item A", "A");
    itemList[1] = new Item("Item B", "B");
    itemList[2] = new Item("Item C", "C");
    return itemList;
  }  
}