我想根据所选项更改菜单的外观。
如何获得使用托管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>
谢谢!
不要使用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>
的event
和execute
属性,因为它们已经代表了默认值)