Search code examples
jsf-2primefacesprettyfaces

Using PrettyFaces action programatically on p:menuitem


I have a PrimeFaces 6.0 menu item hooked to PrettyFaces 3.3.3 like this:

<h:form id="nav"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:prime="http://primefaces.org/ui">

<prime:panelMenu label="Menu">
    <prime:submenu label="SubMenu">
        <prime:menuitem value="page" action="pretty:page"/>
    </prime:submenu>
</prime:panelMenu>
</h:form>

This works fine. But how can I build the menu in Java and achieve the same?

This page

<prime:panelMenu model="#{bean.navMenu}" id="leftNavMenu"/>

gets me the menu structure as expected, but I don't know how to set the action for each menu item.

public MenuModel getNavMenu() {
    MenuModel model = new DefaultMenuModel();
    DefaultSubMenu subMenu = new DefaultSubMenu("sub menu");
    DefaultMenuItem menuItem = new DefaultMenuItem("page");
    // something is missing here
    subMenu.addElement(menuItem);
    model.addElement(subMenu);
    return model;
}

There is no setAction method on DefaultMenuItem.

I tried these:

  • menuItem.setUrl("pretty:page"); --- uses pretty:page as the URL and the browser complains that the address was not understood (understandably)
  • menuItem.setHref("pretty:page"); --- as with setUrl
  • menuItem.setCommand("pretty:page"); gets a NumberFormatException: For input string: "" in the server log
  • menuItem.setOutcome("pretty:page"); gets a URL with a trailing ?com.ocpsoft.mappingId=page, so PrettyFaces does not seem to do its magic, and in turn the page crashes because it does not get its parameters mapped / injected as expected.

How can I set the action from code?

Using setCommand gets this stack trace in the server logs:

[2016-07-28T14:55:42.198-0500] [glassfish 4.1] [SEVERE] [] [javax.enterprise.resource.webcontainer.jsf.context] [tid: _ThreadID=27 _ThreadName=http-listener-1(2)] [timeMillis: 1469735742198] [levelValue: 1000] [[
  java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at org.primefaces.component.menu.BaseMenuRenderer.findMenuitem(BaseMenuRenderer.java:89)
    at org.primefaces.component.menu.BaseMenuRenderer.decode(BaseMenuRenderer.java:67)
    at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
...

Solution

  • In case anybody else comes across this problem: use command in the menu items, and make sure you call generateUniqueIds on the whole menu:

    public MenuModel getNavMenu() {
        MenuModel model = new DefaultMenuModel();
        DefaultSubMenu subMenu = new DefaultSubMenu("sub menu");
        DefaultMenuItem menuItem = new DefaultMenuItem("page");
        menuItem.setCommand("pretty:page");
        subMenu.addElement(menuItem);
        model.addElement(subMenu);
        model.generateUniqueIds();
        return model;
    }