11 Replies Latest reply on Feb 15, 2008 9:45 AM by jinpsu

    Keeping state of a panelMenu between pages

      Hi,

      I'm currently learning JSF/RichFaces and want to create an application using a panelMenu for navigation. I need to be able to keep the collapsed/expanded state of the menu when navigating from page to page. Unless there is a more elegant way of doing this, I'm thinking I'll have to have a session bean keeping track of every group state and use the expanded attribute in panelMenu (let me know if this is the wrong approach).

      I see there are oncollapse and onexpand attributes in panelMenuGroup. How do I bind these to a bean so that I can toggle a boolean value? A simple example would be a huge help.

      Thanks!

        • 1. Re: Keeping state of a panelMenu between pages
          mmichalek

          Have you tried binding the panelMenu's selectedChild?

          • 2. Re: Keeping state of a panelMenu between pages

            Can you keep track of expanding/collapsing of multiple groups with selectedChild? I thought no.

            • 3. Re: Keeping state of a panelMenu between pages
              mmichalek

              Yeah, that probably wouldn't work for multiple group states. I'm also interested to hear suggestions.

              • 4. Re: Keeping state of a panelMenu between pages

                Anyone have an example on how to implement this?

                Thanks.

                • 5. Re: Keeping state of a panelMenu between pages
                  moldovan

                  I'm doing it this way:

                  On the JSF-Page:

                  <rich:panelMenu binding="#{navigationMenu.panelMenu}" width="220" />


                  And the bean looks like:
                  public class NavigationMenu
                  {
                   GuiUtil myGuiUtil = null;
                   HtmlPanelMenu panelMenu = null;
                  
                  
                   public HtmlPanelMenu getPanelMenu()
                   {
                   if(panelMenu == null)
                   {
                   panelMenu = new HtmlPanelMenu();
                  
                   FacesContext facesContext = FacesContext.getCurrentInstance();
                   Application appl = facesContext.getApplication();
                   ELContext elContext = facesContext.getELContext();
                   ExpressionFactory exprFactory = appl.getExpressionFactory();
                  
                   HtmlPanelMenuGroup menuGroup_AccountInfo = new HtmlPanelMenuGroup();
                   menuGroup_AccountInfo.setLabel(myGuiUtil.getMessageResource("menugroup_account"));
                   menuGroup_AccountInfo.setName(myGuiUtil.getMessageResource("menugroup_account"));
                   menuGroup_AccountInfo.setIconCollapsed("triangle");
                   menuGroup_AccountInfo.setIconExpanded("triangleDown");
                  
                   HtmlPanelMenuItem menuItem_AccountInfo = new HtmlPanelMenuItem();
                   menuItem_AccountInfo.setLabel(myGuiUtil.getMessageResource("menugroup_account_products"));
                   menuItem_AccountInfo.setName(myGuiUtil.getMessageResource("menugroup_account_products"));
                   menuItem_AccountInfo.setActionExpression(exprFactory.createMethodExpression(elContext, "navigate_accountinfo", null, new Class[0]));
                  
                   menuGroup_AccountInfo.getChildren().add(menuItem_AccountInfo);
                  
                   panelMenu.getChildren().add(menuGroup_AccountInfo);
                  
                   .......
                   }
                  
                   return panelMenu;
                   }


                  Hope, this helps!

                  • 6. Re: Keeping state of a panelMenu between pages

                    I'm still not sure I understand how you are keeping track of what panelMenuGroups are expanded/collapsed when navigating?

                    • 7. Re: Keeping state of a panelMenu between pages
                      moldovan

                      Whats the problem?

                      For explanation:
                      -) you define your PanelMenu in a bean ( session scoped)
                      -) you can generate your panelmenu when the bean gets initialized (in the constructor). In my Example i do the generation of the panelmenu on the first call of the getter
                      -) define get-method and set-method - with these methods you save/restore your whole panelMenu (with all your expanded/collapsed panelMenuGroups)
                      -) implement panelMenu on JSF-Page via binding

                      There's no trick, thats all!

                      Try it out, and you will understand, how it's working!

                      • 8. Re: Keeping state of a panelMenu between pages

                        Works perfectly! Thanks!

                        • 9. Re: Keeping state of a panelMenu between pages

                          Well, almost perfectly.

                          It seems as if state is only maintained during navigation. However, if I navigate to a page, expand (or collapse) a menu group, then refresh the page, it reverts back to the menu state from when I originally navigated to the page.

                          Is there any way to force the state to be saved when groups are expanded/collapsed?

                          • 10. Re: Keeping state of a panelMenu between pages
                            moldovan

                            The default-mode for expanding/collapsing the menuGroups is client. So its common that you will loose the state of your Menu, if you push the browser-refresh-button.

                            But, what you can do:
                            set the expandMode of all menuGroups to "Ajax - Expand Mode"!

                            You have to insert this code, when you generate your panelMenu from the bean:

                            myMenuGroup.setExpandMode("ajax");


                            This should help!

                            • 11. Re: Keeping state of a panelMenu between pages

                              I read somewhere that it wasn't a good idea to mix ajax and navigation with the panelMenu. I get strange behavior if I set the mode of the panelMenu to anything but server. If I set the panelMenuGroups expandMode to ajax, I get ServletExceptions:

                              javax.servlet.ServletException: FacesEvent of class javax.faces.event.ActionEvent not supported by UIInput