Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces 2 MenuItem Action, ActionExpression or ActionListener

I have an existing working Primefaces 2 MenuBar with the MenuItems defined as follows:

<p:menubar style="width:625px" autoSubmenuDisplay="true">
    <p:submenu label="#{messages.label_home}">       
        <p:menuitem value="#{messages.label_logout}" url="#" icon="ui-icon ui-icon-close"/>
    </p:submenu>

    <p:submenu label="#{messages.label_cockpit}">
        <p:menuitem value="#{messages.label_create}" action="#{cockpitMenuBean.displayCreateDialog}" icon="ui-icon ui-icon-document" ajax="false"/>
        <p:menuitem value="#{messages.label_list}" action="#{cockpitMenuBean.displayList}" icon="ui-icon ui-icon-folder-open" ajax="false"/>
    </p:submenu>

I want to move the menu model from the xhtml to a backing bean as follows:

<p:menubar style="width:625px" autoSubmenuDisplay="true" model="#{cockpitMenuBean.menuModel}"/>

The problem and my question is centered on the action attribute above.

The CockpitMenuBean.displayCreateDialog() returns a string

public String displayCreateDialog() {
    cockpitMenu = new CockpitMenu();
    createDialogVisible = true;
    return "cockpitMenu";
}

menuItem.setAction(arg); which seemed like it should be the same as the action attribute in the XHTML is looking for arg to be a MethodBinding which is deprecated.

menuItem.setActionExpression(arg) is next most likely since the string in the XHTML is an EL expression #{cockpitMenuBean.displayCreateDialog}, but that just returns a String.

menuItem.setActionListener(arg) is deprecated.

I am not connecting the dots in moving from the XHTML to the backing bean in building the menuitems that correspond.

I tried:

MenuItem item1 = new MenuItem();
item1.setValue("Should be first");
item1.setUrl("#");
MethodExpression aEx = expFact.createMethodExpression(elCtx, "#{cockpitMenuBean.displayCreateDialog}", String.class, new Class[0]);
item1.setActionExpression(aEx);
menuModel.addMenuItem(item1);

And the menu item displays but nothing happens when I select it.

like image 604
Mike Oliver Avatar asked Oct 14 '25 14:10

Mike Oliver


1 Answers

Whenever you dynamically create an instance of UIInput or UICommand, then you should give the component a fixed ID. The PrimeFaces MenuItem extends UICommand.

So, this should do:

item1.setId("item1");
like image 149
BalusC Avatar answered Oct 18 '25 04:10

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!