sairn
sairn

Reputation: 500

Using Primefaces 5, should I be able to *dynamically* build a simple p:menu containing only menuitems? ...i.e., without submenu(s)?

QUESTION: Using Primefaces 5, can I dynamically build a simple p:menu containing only menuitems - i.e., without submenu(s)?

For example, I am able to successfully implement this static menu...

    -
    -
    -
    <h3>Static p:menu  (i.e., no submenus) - WORKING </h3>
    <p:menu id="menustatic">
        <p:menuitem value="operation1"  action="#{breadCrumb.navigate('page0')}" />
        <p:menuitem value="operation2"  action="#{breadCrumb.navigate('page1')}" />
        <p:menuitem value="operation3"  action="#{breadCrumb.navigate('page2')}" />
    </p:menu>
    -
    -
    -

But, when I attempt to implement the above menu dyamically from the managed bean, it is not working... i.e.,

the controller method

    -
    -
    -
    private DynamicMenuModel menuModel;
    public MenuModel getMenuModel()
    {
        menuModel = new DynamicMenuModel();

        DefaultMenuItem item = new DefaultMenuItem();
        item.setIcon("ui-icon ui-icon-newwin");
        item.setValue("operation1a");
        item.setCommand("#{breadCrumb.navigate('page0')}");
        menuModel.addElement(item);

        item = new DefaultMenuItem();
        item.setIcon("ui-icon ui-icon-newwin");
        item.setValue("operation1b");
        item.setCommand("#{breadCrumb.navigate('page1')}");
        menuModel.addElement(item);

        item = new DefaultMenuItem();
        item.setIcon("ui-icon ui-icon-newwin");
        item.setValue("operation1c");
        item.setCommand("#{breadCrumb.navigate('page2')}");
        menuModel.addElement(item);

        return menuModel;
    }
    -
    -
    -

the page/tag

    <h3>Dyamic p:menu  (i.e., no submenus) -  NOT WORKING </h3>
    <p:menu id="menudynamic" model="#{menuController.menuModel}" />   

When I click on the "dynamically" built menu's menuitems I receive this error on my console...

    <Jul 17, 2014 1:12:52 PM EDT> <Warning> <javax.enterprise.resource.webcontainer.jsf.lifecycle> <BEA-000000> <For input string: "null"
    java.lang.NumberFormatException: For input string: "null"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:449)
        at java.lang.Integer.parseInt(Integer.java:499)
        at org.primefaces.component.menu.BaseMenuRenderer.findMenuitem(BaseMenuRenderer.java:83)
        at org.primefaces.component.menu.BaseMenuRenderer.decode(BaseMenuRenderer.java:61)
        Truncated. see log file for complete stacktrace
    >       

Is the problem that I am simply misusing the "p:menu" tag in the first place, when I build the static version?... - Is this why my attempting to build a dynamic version of the same menu does not work as I would expect?

Thanks for any help!

:)

Upvotes: 1

Views: 2511

Answers (2)

Jonathan Nonewhynot
Jonathan Nonewhynot

Reputation: 1

You should call generateUniqueIds() on your MenuModel after all MenuElements are added.

Upvotes: 0

odaa
odaa

Reputation: 261

I had the same error and solved it by setting a numerical id for every MenuItem. e.g. item.setId("1");

Maybe it helps for you, too....

Upvotes: 2

Related Questions