天天看點

ICEfaces Note(3)

版權聲明:本文為部落客chszs的原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chszs/article/details/1728801

ICEfaces Note(3)

一、使用ICEfaces的Auto-Complete Component

自動完成元件(Auto-Complete Component)實際上就是ice:selectInputText元件。這個元件提供了一個帶有自動完成功能的增強的文本輸入元件。一旦使用者輸入文本到元件中,元件将提供一個可能比對的彈出清單供使用者選擇。

這個元件在使用者還沒有完成輸入時預測了使用者想要鍵入的短語。這個元件需要開發者在backing bean實作比對搜尋算法。

下面給了使用該元件的例子。

1、selectInputText元件能産生兩種類型的清單(list):

1)字元串資料的清單;

2)任意複雜的子元件清單。

--------------------------

<ice:selectInputText rows="10" width="300" valueChangeListener="#{autoCompleteBean.updateList}">

    <f:selectItems value="#{autoCompleteBean.list}"/>

</ice:selectInputText>

上面的代碼将顯示一個下拉清單引用來比對輸入的文本。

rows屬性定義了當輸入文本時,有多少結果項被列出;

width屬性定義了輸入文本框和下拉清單框的寬度;

valueChangeListener屬性連結backing bean,來控制當輸入清單改變時相應清單的改變。

f:selectItems是JSF的标簽,它連結到backing bean,列出所有的有效值。

下面的代碼将展示怎樣使用元件來産生任意子元件的清單:

ICEfaces Note(3)

<ice:selectInputText rows="6" width="300" listVar="city" 

ICEfaces Note(3)

    valueChangeListener="#{autoCompleteBean.updateList}"

ICEfaces Note(3)

    listValue="#{autoCompleteBean.list}">

ICEfaces Note(3)

    <f:facet name="selectInputText">

ICEfaces Note(3)

        <ice:panelGrid columns="3" style="margin-bottom:-20px;"

ICEfaces Note(3)

            columnClasses="cityCol,stateCol,zipCol">

ICEfaces Note(3)

            <ice:outputText value="#{city.city}"/>

ICEfaces Note(3)

            <ice:outputText value="#{city.state}"/>

ICEfaces Note(3)

            <ice:outputText value="#{city.zip}"/>

ICEfaces Note(3)

        </ice:panelGrid>

ICEfaces Note(3)

    </f:facet>

ICEfaces Note(3)

這個例子顯示了輸入城市名的部分單詞,則下拉清單中顯示出比對的城市名、州名、郵政編碼。

2、建立Backing Beans

在例子中我們使用三個主要的backing beans:

(1)AutoCompleteBean:從AutoCompleteDictionary類存儲收集到的值。其包含的方法是更新清單和從字典清單中得到比對。

(2)AutoCompleteDictionary:從檔案系統中的一個檔案得到字典清單。

(3)City:基本類,作為字典清單的對象。

1)建立字典

對于自動完成元件要工作,必須有一個字典,讓backing beans能查詢和得到比對的變量清單。如下:

ICEfaces Note(3)

<java version="1.4.2_08" class="java.beans.XMLDecoder">

ICEfaces Note(3)

    <object class="java.util.ArrayList">

ICEfaces Note(3)

        <void method="add">

ICEfaces Note(3)

            <object class="com.icesoft.icefaces.tutorial.component.autocomplete.City">

ICEfaces Note(3)

                <void property="areaCode">

ICEfaces Note(3)

                    <string>631</string>

ICEfaces Note(3)

                </void>

ICEfaces Note(3)

                <void property="city">

ICEfaces Note(3)

                    <string>Holtsville</string>

ICEfaces Note(3)
ICEfaces Note(3)

                <void property="country">

ICEfaces Note(3)

                    <string>Suffolk</string>

ICEfaces Note(3)
ICEfaces Note(3)

                <void property="state">

ICEfaces Note(3)

                    <string>New York</string>

ICEfaces Note(3)
ICEfaces Note(3)

                <void property="stateCode">

ICEfaces Note(3)

                    <string>NY</string>

ICEfaces Note(3)
ICEfaces Note(3)

                <void property="zip">

ICEfaces Note(3)

                    <string>00501</string>

ICEfaces Note(3)
ICEfaces Note(3)

            </object>

ICEfaces Note(3)

        </void>

ICEfaces Note(3)

    </object>

ICEfaces Note(3)

</java>

ICEfaces Note(3)

這個檔案可能相當大,是以我們可以zip這個xml檔案以節省空間。