2012-08-10 62 views
1

我想創建一個方法,允許我通過一個帶有inputText字段的JSF頁面進行搜索。我打算從位於託管bean中的數組列表中獲取一個或多個條目,並在我點擊提交後將其顯示在JSF頁面上。 我試圖從這個問題中汲取靈感,但在創建搜索方法我堅持,因爲我在Java中的新手: JSF2 search boxArrayList的JSF2搜索方法

在我的JSF頁面,我打算有這樣的事情,「東西」是方法我失蹤:

<h:form> 
    <h:inputText id="search" value="#{order.something}"> 
     <f:ajax execute="search" render="output" event="blur" /> 
    </h:inputText> 
    <h2> 
     <h:outputText id="output" value="#{order.something}" /> 
    </h2> 
</h:form> 

在我的java文件,我有以下的ArrayList 'orderList',我只是使用字符串:

private static final ArrayList<Order> orderList = 
    new ArrayList<Order>(Arrays.asList(
    new Order("First", "Table", "description here"), 
    new Order("Second", "Chair", "2nd description here"), 
    new Order("Third", "Fridge", "3rd description here"), 
    new Order("Fourth", "Carpet", "4th description here"), 
    new Order("Fifth", "Stuff", "5th description here") 
)); 

希望這是足夠的信息。 我想我必須從頭開始製作一些全新的方法,但我一分鐘也沒有太多。 我會怎樣把它們綁在一起?任何指針將不勝感激:)

〜編輯〜 這裏是我的訂單對象供參考,我假設我在這裏使用一個getters?

public static class Order{ 
    String thingNo; 
    String thingName; 
    String thingInfo; 

    public Order(String thingNo, String thingName, String thingInfo) { 
     this.thingNo = thingNo; 
     this.thingName = thingName; 
     this.thingInfo = thingInfo; 
    } 
    public String getThingNo() {return thingNo;} 
    public void setThingNo(String thingNo) {this.thingNo = thingNo;} 
    public String getThingName() {return thingName;} 
    public void setThingName(String thingName) {this.thingName = thingName;} 
    public String getThingInfo() {return thingInfo;} 
    public void setThingInfo(String thingInfo) {this.thingInfo = thingInfo;} 
    } 

回答

2

首先,你將需要做一些工作與Java平等:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

搜索ArrayList的(也許不是我們的最佳結構,但它的工作)會需要編寫一個函數來處理它,在第一個例子中它使用了commandButton的動作(執行一個方法)。你所擁有的將不會做任何事情,因爲它沒有任何東西可以執行(沒有方法被調用)。

如果你只是在學習JSF,並且你不熟悉Java,我建議保持它簡單直到你學習生命週期。如果你沒有一個可靠的Java背景,那將會非常令人沮喪。

但是,要回答你的問題,你需要用ValueChangeEvent做些事情,因爲你沒有命令(按照他的答案的後半部分)。

您的「搜索方法」將是純Java操作數據結構。一個簡單的實現可能是:

public void searchByFirstValue(String search) 
    { 
    if(search == null || search.length() == 0) 
     return; 

    for(Order order : orderList) 
    { 
     if(order.getFirstValue().contains(search)) //java.lang.String methods via a regular expression 
     setSelectedOrder(order); 

     return; 
    } 
    } 

這是假設你的訂單對象有一個方法getFirstValue()返回一個字符串。我也假設基於你的構造函數,值永遠不爲空(有很多潛在的陷阱)。假設你已經登記在web.xml中的「OrderBean」或者你已經使用了ManagedBean註釋你的JSF可能看起來像:

<h:form> 
    <h:inputText id="search" valueChangeListener="#{orderBean.onChange}"> <!--Use the previous example --> 
     <f:ajax execute="search" render="output" event="blur" /> 
    </h:inputText> 
    <h2> 
     <h:outputText id="output" value="#{orderBean.order}" /> <!-- this is the object from setSelectedOrder(order); --> 
    </h2> 
    </h:form> 

希望這點你在正確的方向。再次,我會堅持使用actions/actionListeners,直到您獲得框架的基礎知識。從那裏很容易移動到非常複雜的行動,沒有太多的工作。我這樣說的原因是它們更易於調試並且很容易理解。

+0

謝謝,我相信這會幫助我很多。那麼我只有三個字符串的getter和setter:thingNo; thingName;和thingInfo ;. – Froob 2012-08-10 15:02:38

+0

不明白setSelectedOrder,即使我導入java.lang,netbeans也不能識別長度。字符串 – Froob 2012-08-10 18:03:12

+0

對不起,這是因爲我現在在跳躍語言,它的方法調用search.length(),而不是屬性(長度)。 Netbeans和eclipse都有很好的功能來提供修復。如果我記得(我可能沒有),Netbeans是ALT-ENTER,並且eclipse是用於修復的ctrl-space/ctrl-1。上面的代碼更多的是一個草圖而不是工作代碼。 – 2012-08-10 18:32:59