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");
--- usespretty:page
as the URL and the browser complains that the address was not understood (understandably)menuItem.setHref("pretty:page");
--- as withsetUrl
menuItem.setCommand("pretty:page");
gets aNumberFormatException: For input string: ""
in the server logmenuItem.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)
...
org.primefaces.component.menuitem.UIMenuItem
. It hassetAction
for sure. – Emil Sierżęgap:commandLink
? – Kukeltjep:commandLink
? And you can debug what happens when doing amenuItem.setCommand("pretty:page");
Where does the NFE occur (stacktrace) and why is the one with the setOutcome wrong? Seems like something adds the trailing part and it is not PrimeFaces. – KukeltjesetCommand
on the menu items and then callgenerateUniqueIds()
on theMenuModel
before returning it. – Robert