1 2 3 4 Previous Next 49 Replies Latest reply on Jul 12, 2011 1:41 AM by pankaj_jboss Go to original post
      • 15. Re: How to build rich:dropDownMenu dynamically

        Thanks for your ideas in order to assign an action to a HtmlMenuItem with MethodExpression/setActionExpression instead of deprecated MethodBinding/setAction.

        Please, would you say me how to use MethodExpression/setActionExpression in this situation ?

        I have unsuccessfully tried as follows:

        import javax.el.MethodExpression;
        ...
        HtmlMenuItem menuItem = new HtmlMenuItem();
        menuItem.setValue("Sec. Change Passsword");
        MethodExpression methodExp = new MethodExpression("#{otherBean.action}");
        menuItem.setActionExpression(methodExp);


        The problem is that the constructor of MethodExpression does not have parameters: how can I indicate the action ?

        Thanks



        • 16. Re: How to build rich:dropDownMenu dynamically
          jinkam

          is it possible to apply a4j:repeat tag to repeat menuItem?

          • 17. Re: How to build rich:dropDownMenu dynamically
            ne

            I would like to use the <a4j:repeat> tag to put dynamic MenuItems into the DropDownMenu but I fail doing this. Creating the MenuItems dynamically in Java as described above in this thread works fine but feels like a workaround to me. I would like to have the layouts in XML if possible.

            <h:form>
            <h:panelGrid ...>
            <rich:dropDownMenu value="Something with options">
            <a4j:repeat value="#{anyModel.options}" var="oneOption">
            <rich:menuItem value="#{oneOption}"/>
            </a4j:repeat>
            </rich:dropDownMenu>
            </h:panelGrid>
            </h:form>

            ...could someone tell me why this approach does not work?

            • 18. Re: How to build rich:dropDownMenu dynamically
              dmitry.demyankov

              I guess a4j:repeat won't do the job, not sure though..

              You can use binding for your dropDownMenu and create menuItems in backing bean.. the following code worked for me

              import org.richfaces.component.html.HtmlDropDownMenu;
              import org.richfaces.component.html.HtmlMenuItem;
              ...
              private HtmlDropDownMenu dropDownMenu = new HtmlDropDownMenu();
              ...
              public TestPage() {
               HtmlMenuItem item = new HtmlMenuItem();
               item.setValue("item");
              
               FacesContext fc = FacesContext.getCurrentInstance();
               ELContext el = fc.getELContext();
               MethodExpression me = fc.getApplication().getExpressionFactory().createMethodExpression(el, "#{testPage.testMeth}", null, new Class[0]);
               item.setActionExpression(me);
              
               getDropDownMenu().getChildren().add(item);
              }
              


              I'm just not sure about
              new Class[0]
              maybe there's a better way..

              and in JSP page

              <rich:dropDownMenu value="Menu" binding="#{testPage.dropDownMenu}">


              So you just need to iterate through your list of items and add them to the dropDownMenu

              • 19. Re: How to build rich:dropDownMenu dynamically
                ne

                I gave a bad example earlier so now I will make up for my misstake. The following code does not work...


                <h:form>
                 <h:panelGrid ...>
                 <rich:dropDownMenu value="Something with options">
                 <a4j:repeat value="#{anyModel.options}" var="oneOption">
                 <rich:menuItem binding="#{oneOption}" value="#{oneOption}"/>
                 </a4j:repeat>
                 </rich:dropDownMenu>
                 </h:panelGrid>
                </h:form>
                


                ...and the reason is that the oneOption variable is not created when the menuItem is to be created. Right after creation of menuItem, the binding is set but since the oneOption is null (the repeat component is only created but not rendered). To set every element in i anyModel.options list dynamically, I used a binding on the dropDownMenu component and then extended the tree programmatically from there.

                <h:form>
                 <h:panelGrid ...>
                 <rich:dropDownMenu value="Something with options">
                ...made the repeat logic programmatically in the binding method </rich:dropDownMenu>
                 </h:panelGrid>
                </h:form>
                



                My conclusion is that repeat cannot help in setting bindings of component to backing bean dynamically. Please let me know if this is not the correct conclusion.



                • 20. Re: How to build rich:dropDownMenu dynamically
                  ilya_shaikovsky

                  You shouldn't use repeat components for such cases. You should use c:forEach

                  • 21. Re: How to build rich:dropDownMenu dynamically
                    dsleeper

                    I cannot seem find the necessary includes for this code:

                    FacesContext fc = FacesContext.getCurrentInstance();
                    ELContext el = fc.getELContext();
                     MethodExpression me = fc.getApplication().getExpressionFactory().createMethodExpression(el, "#{testPage.testMeth}", null, new Class[0]);


                    My IDE says that:
                    the method getELContext is undefined for the type FacesContext
                    the method "fc.getApplication().getExpressionFactory()" is undefined for the type Application

                    Perhaps I'm missing some jars in my classpath here? I'm using Myfaces and Richfaces together

                    Is there any other way to create a methodbinding to use in a menuItem?


                    • 22. Re: How to build rich:dropDownMenu dynamically
                      atao

                      Did you tried:


                      ExpressionFactory factory = SeamExpressionFactory.INSTANCE;

                      FacesContext fc = FacesContext.getCurrentInstance();
                      ELContext el = fc.getELContext();

                      MethodExpression expression = factory.createMethodExpression(el, menuItem.getAction(), Object.class,new Class<?>[0]);


                      It works for me (Seam 2, cvs 10/06/07)

                      • 23. Re: How to build rich:dropDownMenu dynamically
                        dsleeper

                        I'm not using seam.

                        There must be a way to do this without seam I guess?

                        • 24. Re: How to build rich:dropDownMenu dynamically
                          mores

                           

                          I have successfully followed your indications for building a dropDownMenu dynamically, but I have problems assigning an action with a parameter to a HtmlMenuItem.

                          Here is the page code:

                          <rich:dropDownMenu binding="#{menuBean.myMenu}" >
                          </rich:dropDownMenu>


                          Here is the Java code in the menuBean:

                          private HtmlDropDownMenu getMyMenu() {
                           HtmlDropDownMenu menu = new HtmlDropDownMenu();
                           menu.setStyle("border:1px solid #BCD1FF");
                           menu.setValue("Security Actions");
                          
                           HtmlMenuItem menuItem = new HtmlMenuItem();
                           String subOption = "Sec. Change Passsword";
                           menuItem.setValue(subOption);
                          
                           Application app = FacesContext.getCurrentInstance().getApplication();
                           java.lang.Class[] params = { java.lang.String.class };
                           MethodBinding mb = app.createMethodBinding ("#{otherBean.action}", params );
                           ??? java.lang.Object[] args = { new java.lang.String( subOption ) };
                           ??? mb.invoke( javax.faces.context.FacesContext.getCurrentInstance(), args );
                           menuItem.setAction (mb);
                          
                           menu.getChildren().add(menuItem);
                           return menu;
                           }



                          I see where to set the Parameter signatures, but where do I actually set the parameter ?

                          No matter what I try I keep getting: java.lang.IllegalArgumentException: wrong number of arguments

                          • 25. Re: How to build rich:dropDownMenu dynamically
                            dmitry.demyankov

                            Why do you use deprecated createMethodBinding instead of createMethodExpression?

                            • 26. Re: How to build rich:dropDownMenu dynamically
                              dsleeper

                              Can anybody from the richfaces team show a code example of how this should be done?

                              Ie a complete createMethodExpression code example?

                              • 27. Re: How to build rich:dropDownMenu dynamically
                                mores

                                 

                                "dmitry.demyankov" wrote:
                                Why do you use deprecated createMethodBinding instead of createMethodExpression?


                                If I am still using JSF 1.1.x then it is not deprecated :-)

                                • 28. Re: How to build rich:dropDownMenu dynamically
                                  mores

                                   

                                  "Dsleeper" wrote:
                                  Can anybody from the richfaces team show a code example of how this should be done?

                                  Ie a complete createMethodExpression code example?


                                  I finally got this to work:

                                  <rich:dropDownMenu binding="#{webmenu.myMenu}" >
                                  </rich:dropDownMenu>
                                  


                                  public class WebMenu
                                  {
                                   private static org.slf4j.Logger _log = org.slf4j.LoggerFactory.getLogger( WebMenu.class );
                                  
                                   private String lastItem = "None";
                                  
                                   public org.richfaces.component.html.HtmlDropDownMenu getMyMenu()
                                   {
                                   _log.trace( "Creating menu" );
                                  
                                   org.richfaces.component.html.HtmlDropDownMenu menu =
                                   new org.richfaces.component.html.HtmlDropDownMenu();
                                   menu.setValue( "Security Actions" );
                                  
                                   org.richfaces.component.html.HtmlMenuItem menuItem =
                                   new org.richfaces.component.html.HtmlMenuItem();
                                   String subOption = "Sec. Change Passsword";
                                   menuItem.setValue( subOption );
                                  
                                   javax.faces.application.Application app =
                                   javax.faces.context.FacesContext.getCurrentInstance().getApplication();
                                   javax.faces.el.MethodBinding mb = app.createMethodBinding( "#{webmenu.onItemClick}", new Class[] { javax.faces.event.ActionEvent.class } );
                                   menuItem.setActionListener( mb );
                                  
                                   menu.getChildren().add( menuItem );
                                   return( menu );
                                   }
                                  
                                   public void onItemClick( javax.faces.event.ActionEvent event )
                                   {
                                   Object obj = event.getSource();
                                  
                                   if( obj instanceof org.richfaces.component.html.HtmlMenuItem )
                                   {
                                   org.richfaces.component.html.HtmlMenuItem item =
                                   (org.richfaces.component.html.HtmlMenuItem)obj;
                                   if( item != null )
                                   {
                                   lastItem = item.getValue().toString();
                                   _log.trace( "Clicked On: " + lastItem );
                                   }
                                   }
                                   }
                                  }
                                  


                                  They key is to use actionListener and NOT action. These posts helped:

                                  http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=82&t=002888
                                  http://balusc.blogspot.com/2006/06/communication-in-jsf.html

                                  • 29. Re: How to build rich:dropDownMenu dynamically
                                    nevez

                                    hello, i have the following problem:
                                    http://www.jboss.org/index.html?module=bb&op=viewtopic&t=123268

                                    in my .ear file there are:
                                    ./
                                    ./lib/richfaces-ui
                                    ./lib/richfaces-impl

                                    because if i dont include them i got class not found exception: UIDropDownMenu.

                                    also:

                                    ./war/WEB-INF/lib/richfaces-impl
                                    ./war/WEB-INF/lib/richfaces-ui

                                    maybe the problem is with classloading ?