I am working at a primefaces dynamic menubar. The content of the menubar should be updated as a commandbutton is pressed.
Here is my very simple xhtml page
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>CoSMo</title>
</h:head>
<h:body>
#{simpleMenuBacking.init()}
<h:form>
<p:commandButton value="Submit" action="#{simpleMenuBacking.updateModel()}" update="mb1"/>
<p:growl id="messages" showDetail="false"/>
<p:menubar id="mb1" model="#{simpleMenuBacking.model}" />
</h:form>
</h:body>
And my (accordingly simple) backing bean:
@Named
@SessionScoped
public class SimpleMenuBacking implements Serializable {
private DefaultMenuModel model;
public void init() {
Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.init()] Starting.");
model = new DefaultMenuModel();
String myMenuEntry = "MENU ENTRY " + new Random().nextInt();
Logger.getLogger(UserBacking.class.getName()).info(myMenuEntry);
model.addElement(new DefaultSubMenu(myMenuEntry));
}
public SimpleMenuBacking() {
}
public DefaultMenuModel getModel() {
Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.getModel()] Starting.");
return model;
}
public void updateModel() {
Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.updateModel()] Starting.");
String myMenuEntry = "MENU ENTRY " + new Random().nextInt();
model.addElement(new DefaultSubMenu(myMenuEntry));
Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.updateModel()] Over.");
}
}
Unfortunately, as i press the commandbutton, the model of the menubar is updated, but the changes are not rendered within the menubar. I figured out that the following exception might almost certainly be the cause of the issue:
SEVERE: Error Rendering View[/index.xhtml]
java.lang.NullPointerException
at org.primefaces.component.menubar.MenubarRenderer.encodeSubmenuIcon(MenubarRenderer.java:60)
at org.primefaces.component.tieredmenu.TieredMenuRenderer.encodeSubmenu(TieredMenuRenderer.java:161)
at org.primefaces.component.tieredmenu.TieredMenuRenderer.encodeElements(TieredMenuRenderer.java:127)
at org.primefaces.component.tieredmenu.TieredMenuRenderer.encodeMenu(TieredMenuRenderer.java:78)
at org.primefaces.component.menubar.MenubarRenderer.encodeMarkup(MenubarRenderer.java:50)
at org.primefaces.component.menu.BaseMenuRenderer.encodeEnd(BaseMenuRenderer.java:108)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:559)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
...
How is this caused and how can I solve it? I am willing to give further details about my code and/or project settings. Thanks.
Finally, I figured out the problem.
In this discussion it is explained that, if the DefaultSubMenu objects do not have an id, the rendering of the menubar fails. Just assing an id to each DefaultSubMenu object, and you are done.
Alternatively, you can invoke
menuModel.generateUniqueIds();
to let the model generate unique ids for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With