I am trying to get a Primefaces MenuItem [within a Menubar] to emit an ActionEvent and call a method when pressed, however, the listening methods are never called, and I have no idea why:
xhtml page:
<h:body>
<h:form>
<p:menubar model="#{testBean.model}" />
<h:commandButton value="submit" />
<h:outputText value="#{testBean.confirmation}" />
</h:form>
</h:body>
</html>
jsf bean:
@ManagedBean
public class TestBean implements ActionListener{
MenuModel model = new DefaultMenuModel();
private String confirmation = "negative";
public TestBean() {
MenuItem item = new MenuItem();
item.setValue("Click");
item.setUrl("#");
item.addActionListener(this);
model.addMenuItem(item);
}
@Override
public void processAction(ActionEvent event)
throws AbortProcessingException {
// EXECUTION NEVER REACHES HERE!
confirmation = "positive";
}
public MenuModel getModel() {
return model;
}
public String getConfirmation() {
return confirmation;
}
}
It might help to visualise the very simple output:

I’m using Primefaces 3.1.0, and JSF 2.0
The first problem with the original code was that I needed a unique Id. I discovered this early on, but it lead to another very annoying issues: the action listener would be called, but the page would not update. To solve this issue (as described here), you either disable ajax on the MenuItem or customise how it updates.
The correct constructor code is: