1

我想根据所选项目更改菜单的外观。

如何使用托管 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>

谢谢!

4

1 回答 1

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

(请注意,我省略了eventexecute属性,<f:ajax>因为它们已经代表了默认值)

于 2013-01-10T14:38:26.630 回答