2013-01-10 39 views
1

我想根據所選項目更改菜單的外觀。如何使用selectOneListBox和AJAX動態更改panelGroup

如何使用託管bean獲取selectonelistbox的值?

豆源:

public void selectOneMenuListener(ValueChangeEvent event) { 
    Object newValue = (String) event.getNewValue(); 
    selectedMenu = newValue.toString(); 
} 

public String getSelectedMenu() { 
    return selectedMenu; 
} 

public void setSelectedMenu(String selectedMenu) { 
    this.selectedMenu = selectedMenu; 
} 

頁:

<h:panelGroup id="panel"> 
    <h:selectOneListbox id="katProduktu" valueChangeListener="#{produkt_KatBean.selectOneMenuListener}"> 
     <f:selectItems value="#{produkt_KatBean.produkt_KatAllList}" 
      var="pk" itemLabel="#{pk.symbol}" itemValue="#{pk.id}"/> 
     <f:ajax render="produktMenu" event="change" execute="@this" /> 
    </h:selectOneListbox> 
</h:panelGroup> 

<h:panelGroup layout="block" id="produktMenu"> 
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==1}"> 
     Menu 1 
    </h:panelGroup> 
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==2}"> 
     Menu 2 
    </h:panelGroup> 
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==3}"> 
     Menu 3 
    </h:panelGroup>  
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==4}"> 
     Manu 4 
    </h:panelGroup> 
</h:panelGroup> 

謝謝!

回答

0

請勿使用valueChangeListener。這是工作的錯誤工具。只需將該屬性綁定到菜單組件的value屬性並讓rendered屬性在該屬性上截取即可。這樣你根本不需要聽衆。您的rendered比較也不適用於字符串。你應該使用== '1'而不是== 1等等,但是這很笨拙,只是讓它Long(或Integer)。

<h:panelGroup id="panel"> 
    <h:selectOneListbox id="katProduktu" value="#{produkt_KatBean.selectedMenu}"> 
     <f:selectItems value="#{produkt_KatBean.produkt_KatAllList}" 
      var="pk" itemLabel="#{pk.symbol}" itemValue="#{pk.id}"/> 
     <f:ajax render="produktMenu" /> 
    </h:selectOneListbox> 
</h:panelGroup> 

<h:panelGroup layout="block" id="produktMenu"> 
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==1}"> 
     Menu 1 
    </h:panelGroup> 
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==2}"> 
     Menu 2 
    </h:panelGroup> 
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==3}"> 
     Menu 3 
    </h:panelGroup>  
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==4}"> 
     Manu 4 
    </h:panelGroup> 
</h:panelGroup> 

只需

private Long selectedMenu; // Or Integer? Should be the same type as #{pk.id}. 

// +getter +setter 

(請注意,我省略<f:ajax>eventexecute屬性,因爲它們代表已默認值)

+0

感謝您的答覆,但這種方法不不工作:( – insict

+0

準確地說,這段代碼拷貝到一個完全空白的頁面,只有一個'',對我有效。你很好地理解了答案。另外,請嘗試從開發者的角度而不是從最終用戶的角度闡述「不起作用」。 ajax請求是否發送?調用者是否被調用?吸氣劑是否被調用?房產類型是否合適? ajax響應是否正確?服務器日誌沒有錯誤?瀏覽器控制檯沒有錯誤?等等 – BalusC

+0

我收到Java異常:javax.faces.component.UpdateModelException:javax.el.PropertyNotFoundException:/index.xhtml @ 60,45 value =「#{produkt_KatBean.selectedMenu}」:屬性'selectedMenu'不可寫鍵入pl.com.pir.beans.Produkt_KatBean – insict