2012-11-10 19 views
0

如何重新填充我的列表(stationaryList)?我的代碼有什麼問題?如果我選擇value = CDL或MEL或NEF,則列表中沒有任何更改。謝謝。如何在selectOneMenu中重新填充我的列表?

<h:form id="frmCreateNewStationary"> 
<ui:param name="s" value="#{myController.selected}"/> 
<h:panelGrid columns="2"> 
    <h:outputLabel value="Type" for="idType" /> 
    <h:selectOneMenu id="idType" value="#{s.stationaryid.type}"> 
     <f:selectItem itemLabel="Select ..." noSelectionOption="true" /> 
     <f:selectItem itemLabel="CDL" itemValue="CDL" /> 
     <f:selectItem itemLabel="MEL" itemValue="MEL" /> 
     <f:selectItem itemLabel="NEF" itemValue="NEF" /> 
     <f:ajax event="change" listener="#{myController.changeStationaryCodeList}" render="idCode" execute="@this" /> 
    </h:selectOneMenu> 
    <h:outputLabel value="Code" for="idCode" /> 
    <h:selectOneMenu id="idCode" value="#{s.stationaryid.code}"> 
     <f:selectItem itemLabel="Select ..." noSelectionOption="true" /> 
     <f:selectItems value="#{myController.stationaryList}" /> 
    </h:selectOneMenu> 
</h:panelGrid> 
</h:form> 

的Java

private List<Stationary> stationaryList = null; 
public List<Stationary> getStationaryList() { 
    stationaryList = getCode(); 
    return stationaryList; 
} 

public void setStationaryList(List<Stationary> stationaryList) { 
    this.stationaryList = stationaryList; 
} 

...

public List<Stationary> getCode() { 
    //"Stationary.ccc", query = "SELECT s.code FROM Stationary s"), 
    EntityManager emf = facade.getEntityManager(); 
    Query query = emf.createNamedQuery("Stationary.ccc"); 
    return query.getResultList();   
} 

...

public List<Stationary> getStationaryCodeItems(String type) { 
    //"Stationary.code", query = "SELECT s.code FROM Stationary s WHERE s.type = :type"), 
    EntityManager emf = facade.getEntityManager(); 
    Query query = emf.createNamedQuery("Stationary.code"); 
    query.setParameter("type", type); 
    return query.getResultList(); 
} 

AjaxBehaviorEvent看起來是這樣的:

public void changeStationaryCodeList(AjaxBehaviorEvent ev) { 
    stationaryList.clear(); 
    String type = (String) ((UIInput) ev.getComponent()).getValue(); 
    System.out.println("Test__changeStationaryCodeList -- state == " + type); 
    stationaryList = getStationaryCodeItems(type); 
    //setStationaryList(stationaryList); 
} 

回答

0

您的<h:selectOneMenu id="idType">沒有value屬性集。將它綁定到您的後臺bean中的值,因爲您已經在使用<h:selectOneMenu id="idCode" value="#{s.stationaryid.code}">並且可以在您的ajax偵聽器中將該屬性用作提交的值。我也有一種感覺,你可能沒有在這裏使用正確的範圍。使用@ViewScoped註釋標註您的支持bean,以便在您的AJAX中免費獲得結果

與此無關,爲什麼您有一個名爲「s」的支持bean?這隻會讓你的代碼很難閱讀。

+0

嗨,我剛剛更新了我的代碼,抱歉太簡短了。我有2種形式:1編輯,1創建新的。使用編輯窗體它運行良好,但對於創建新窗體,它顯示錯誤:javax.el.PropertyNotFoundException:/jsf/aircraftstationary/Create.xhtml @ 24,90 value =「#{s.stationaryid.type}」:目標不可訪問,'null'返回null – Peter

+0

所以,如果我刪除:value =「#{s.stationaryid.type}」顯示沒有錯誤,但我的List不能改變,不知爲什麼 – Peter

+0

@Peter,我沒有意味着你應該複製粘貼相同的'value =「#{s.stationaryid.type}」'到'idType'。我的意思是你應該在你的支持bean(帶有相應的getter和setter)上創建一個'String'屬性,並將它綁定到下拉菜單中,如'value =「#{yourBackingBean.theVariable}」'。你有沒有把你的支持bean改成'@ ViewScoped' bean?你從某處複製了這段代碼嗎? – kolossus