目标:
我们需要有一个展开/折叠按钮来展开或折叠所有SubMenus - 这似乎可以工作,除非其中一个州不同。
E.g:
菜单状态:true(展开),true(展开),true(展开),true(展开)将更改为false(折叠),false(折叠),false(折叠),false(折叠),反之亦然用我们当前的代码。
HOWEVER - > true(扩展), false(折叠),true(展开),true(展开)不会更改为false(折叠),false(折叠),false(折叠),false(折叠)和反之亦然,我们目前的代码。
问题:
我有一个问题,看起来好像视图(xhtml)页面没有更新JSF支持bean
代码:
XHTML:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui" >
<h:head>
<title>Website</title>
</h:head>
<h:body>
<p:growl id="messages"/>
<p:layout id="fullpagelayout" fullPage="true" >
<p:layoutUnit id="layoutNorth" position="north" resizable="true" size="120" style="min-height: 120px; height: 120px;" >
<ui:include id="northHeader" src="header.xhtml" />
</p:layoutUnit>
<p:layoutUnit id="layoutWest" position="west" collapsible="true" resizable="true" style="min-width: 150px;">
<h:form id="menuForm">
<p:commandButton id="expandBtn" value="Expand" action="#{mainMenuBean.changeMenuState(true)}" async="true" update="menuForm:mainMenu" styleClass="expandCollapseBtn" style="margin-bottom: 2px;" />
<p:commandButton id="collapseBtn" value="Collapse" action="#{mainMenuBean.changeMenuState(false)}" async="true" update="menuForm:mainMenu" styleClass="expandCollapseBtn" style="margin-bottom: 2px;" />
<p:panelMenu id="mainMenu" model="#{mainMenuBean.model}" />
</h:form>
</p:layoutUnit>
<p:layoutUnit id="layoutCenter" position="center" style="padding: 5px; margin: 5px;" >
<ui:include id="centerPanel" src="centerPanel.xhtml" />
</p:layoutUnit>
<p:layoutUnit id="layoutSouth" position="south" resizable="true" closable="true" size="30" >
<ui:include id="southPanel" src="status.xhtml" />
</p:layoutUnit>
</p:layout>
</h:body>
</html>
支持bean:
package /*PACKAGE*/
/*imports*/
@ManagedBean
@SessionScoped
public class MainMenuBean implements Serializable {
private MenuModel model;
private MenuStateController stateController;
private String updateParam;
public MainMenuBean() {
initBeans();
}
private void initBeans() {
buildMainMenu();
}
public MenuModel getModel() {
return model;
}
public void setModel(MenuModel model) {
this.model = model;
}
public void buildMainMenu() {
model = new DefaultMenuModel();
stateController = new MenuStateController();
updateParam = ":panelContents";
// File Submenu and menu Items
DefaultSubMenu fileSubMenu = new DefaultSubMenu("File");
addMenuItem(fileSubMenu, "Logout", "logoutPage", "ui-icon-home",
"/login/login.xhtml?faces-redirect=true", false, false, updateParam, stateController);
model.addElement(fileSubMenu);
// Tools Submenu and menu Items
DefaultSubMenu toolsSubMenu = new DefaultSubMenu("Tools");
addMenuItem(toolsSubMenu, "Tool Page", "toolPage", "ui-icon-wrench",
"#{menuStateController.setPage('/content/tool.xhtml')}", true, true, updateParam, stateController);
model.addElement(toolsSubMenu);
// Search Submenu and menu Items
DefaultSubMenu searchSubMenu = new DefaultSubMenu("Search");
addMenuItem(searchSubMenu, "Search", "searchPage", "ui-icon-search",
"#{menuStateController.setPage('/content/search.xhtml')}", true, true, updateParam, stateController);
model.addElement(searchSubMenu);
// Help Submenu and menu Items
DefaultSubMenu helpSubMenu = new DefaultSubMenu("Help");
addMenuItem(helpSubMenu, "About", "aboutPage", "ui-icon-help",
"#{menuStateController.setPage('/content/about.xhtml')}", true, true, updateParam, stateController);
model.addElement(helpSubMenu);
}
private void addMenuItem(DefaultSubMenu subMenu, String menuItemName, String id, String icon,
String commandName, boolean isAjax, boolean isAsync, String Update, MenuStateController stateController) {
NEWMenuItem item = new NEWMenuItem(menuItemName, id, stateController);
item.setId(id);
item.setIcon(icon);
item.setCommand(commandName);
item.setAjax(isAjax);
item.setAsync(isAsync);
item.setUpdate(Update);
subMenu.addElement(item);
}
public void changeMenuState(boolean expanded) {
for (MenuElement menuElement : model.getElements()) {
if(menuElement.getClass() == DefaultSubMenu.class){
if(menuElement instanceof DefaultSubMenu){
DefaultSubMenu subMenu = (DefaultSubMenu) menuElement;
if(!subMenu.isExpanded() == expanded){
subMenu.setExpanded(expanded);
}
}
}
}
}
}
所以基本上我想要“展开”或“折叠”按钮来展开或折叠所有子菜单,无论全部或部分是否已经展开或折叠。
希望这是有道理的!