posts - 6,  comments - 7,  trackbacks - 0

          1.       HTML代碼

          最終實現(xiàn)的效果代碼,如下所示:

          <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標(biāo)簽:

          <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屬性對應(yīng)的變量是枚舉DeviceType的一個實例。

          (b)    converter="voutputDeviceTypeConverter"

          類型轉(zhuǎn)換器,在在"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代碼如下:

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

          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對應(yīng)變量必須是SelectItemjavax.faces.model.SelectItem)列表,所以有必要將DeviceType類型實例的值和對應(yīng)String類型值,封裝在一個SelectItem實例中。實現(xiàn)代碼如下:

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

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


          網(wǎng)站導(dǎo)航:
           
          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 砀山县| 湘潭县| 乡城县| 廊坊市| 卓资县| 惠东县| 北流市| 澜沧| 榆中县| 娱乐| 海南省| 永州市| 湖州市| 紫云| 理塘县| 五河县| 个旧市| 镇江市| 潞城市| 华亭县| 绍兴县| 汉川市| 渭南市| 静乐县| 寿宁县| 凯里市| 贺兰县| 清河县| 柳林县| 无锡市| 武宁县| 九江县| 奈曼旗| 厦门市| 烟台市| 屏南县| 临漳县| 宜川县| 婺源县| 右玉县| 思南县|