posts - 6,  comments - 7,  trackbacks - 0

          1.       HTML代碼

          最終實現的效果代碼,如下所示:

          <select>

          <option selected="selected" value="Monitor">Monitor</option>

          <option value="VCR">VCR</option>

          <option value="Standard Device">Standard Device</option>

          <option value="Smart Device">Smart Device</option>

          <option value="Trunk">Trunk</option>

          <option value="Standby VCR">Standby VCR</option>

          </select>

          2.       enum代碼

          publicenum DeviceType {

              @XmlEnumValue("Monitor")

              MONITOR("Monitor"),

              VCR("VCR"),

              @XmlEnumValue("Standard Device")

              STANDARD_DEVICE("Standard Device"),

              @XmlEnumValue("Smart Device")

              SMART_DEVICE("Smart Device"),

              @XmlEnumValue("Trunk")

              TRUNK("Trunk"),

              @XmlEnumValue("Standby VCR")

              STANDBY_VCR("Standby VCR");

              privatefinal String value;

              DeviceType(String v) {

                  value = v;

              }

              public String value() {

                  returnvalue;

              }

              publicstatic DeviceType fromValue(String v) {

                  for (DeviceType c: DeviceType.values()) {

                      if (c.value.equals(v)) {

                          return c;

                      }

                  }

                  thrownew IllegalArgumentException(v);

              }

          }

          3.       JSF標簽:

          <h:selectOneMenu value="#{voutputType.DEVICETYPE}" converter="voutputDeviceTypeConverter">

          <f:selectItems value="#{voutput.deviceTypeList}"/>

          </h:selectOneMenu>

          主要有三個部分組成

          (a)     value="#{voutputType.DEVICETYPE}"

          javabean ,voutputType中的DEVICETYPE屬性,確定html代碼中<option selected="selected" value="Monitor">項的值

          voutputType配置信息在"WebRoot"WEB-INF"faces-config.xml

          <managed-bean>

                 <managed-bean-name>voutputType</managed-bean-name>

                 <managed-bean-class>

                     cn.com.gentek.imatrix.xml.jaxb.voutput.ObjVOutputType

                 </managed-bean-class>

                 <managed-bean-scope>session</managed-bean-scope>

          </managed-bean>

              其中DEVICETYPE屬性對應的變量是枚舉DeviceType的一個實例。

          (b)    converter="voutputDeviceTypeConverter"

          類型轉換器,在在"WebRoot"WEB-INF"faces-config.xml配置如下:

          <converter>

              <converter-id>voutputDeviceTypeConverter</converter-id>

              <converter-class>

                 cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter

              </converter-class>

          </converter>

          cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter代碼如下:

          (實現< select><option>String類型值,與DeviceType類型之間的轉換)

          publicclass VoutDeviceTypeConverter implements Converter {

              public Object getAsObject(FacesContext context, UIComponent component, String value) {

                 DeviceType result = null;

                 if (value == null || value.length() < 1) {

                     result = null;

                 } else

                     result = DeviceType.fromValue(value);

                 returnresult;

              }

              public String getAsString(FacesContext context, UIComponent component, Object value) {

                 String result = null;

                 if (value != null) {

                     if (value instanceof DeviceType) {

                        DeviceType temp = (DeviceType) value;

                        result = temp.value();

                     }

                 }

                 return result;

              }

          }

          (c)      <f:selectItems value="#{voutput.deviceTypeList}"/>(重點)

          由于deviceTypeList對應變量必須是SelectItemjavax.faces.model.SelectItem)列表,所以有必要將DeviceType類型實例的值和對應String類型值,封裝在一個SelectItem實例中。實現代碼如下:

          ArrayList<SelectItem> deviceTypeList = new ArrayList<SelectItem>();

          for (int i = 0; i < DeviceType.values().length; i++) {

              deviceTypeList.add(new SelectItem(DeviceType.values()[i],

                     DeviceType.values()[i].value()));

          }

          posted on 2008-03-03 16:15 zhan 閱讀(2128) 評論(3)  編輯  收藏

          FeedBack:
          # re: JSF web編程:通過enum數組生成 下拉選擇列表
          2008-03-03 20:00 | 魔域私服
          希望多出點這些實例  回復  更多評論
            
          # re: JSF web編程:通過enum數組生成 下拉選擇列表
          2008-03-04 10:40 | Jerome
          通過enum生成下拉列表 比其他從backbean數組中獲取有什么好處嗎?  回復  更多評論
            
          # re: JSF web編程:通過enum數組生成 下拉選擇列表
          2008-03-04 17:07 | zhan
          @Jerome
          學java的時間不長,對你說的backbean數組不是很了解。
          還有,這里只是提供參考而已,因該還有很多其他的實現方法,如果能列出具體示例的話,應該很容易能看出他們的優缺點。  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乌拉特前旗| 定西市| 铅山县| 新化县| 吉安市| 开平市| 公主岭市| 固始县| 慈利县| 济南市| 韶关市| 浠水县| 文水县| 抚宁县| 仁化县| 北宁市| 凤庆县| 修水县| 察隅县| 沈阳市| 仁化县| 新平| 双辽市| 大英县| 马边| 通州市| 新建县| 新田县| 蓝山县| 剑川县| 宁南县| 略阳县| 安龙县| 鹤庆县| 百色市| 虞城县| 呼玛县| 浦城县| 乐都县| 平果县| 西乌珠穆沁旗|