I'm new to Primefaces, and I'm following the User Guide of Version 5.0. I've successfully built a static menu. Now I'm trying to build a very simple dynamic menu, basically copy-pasting the example on the user guide (page 294, if you are interested). But it's not working.
This is my XHTML page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>test-primefaces</title>
</h:head>
<h:body>
<h:form>
<p:menu model="#{menuBean.model}" />
</h:form>
</h:body>
This is my MenuBean.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;
@ManagedBean
@SessionScoped
public class MenuBean {
private MenuModel model;
public MenuBean() {
model = new DefaultMenuModel();
//First submenu
DefaultSubMenu firstSubmenu = new DefaultSubMenu("Dynamic Submenu");
DefaultMenuItem item = new DefaultMenuItem("External");
item.setUrl("http://www.primefaces.org");
item.setIcon("ui-icon-home");
firstSubmenu.addElement(item);
model.addElement(firstSubmenu);
//Second submenu
DefaultSubMenu secondSubmenu = new DefaultSubMenu("Dynamic Actions");
item = new DefaultMenuItem("Save");
item.setIcon("ui-icon-disk");
item.setCommand("#{menuBean.save}");
item.setUpdate("messages");
secondSubmenu.addElement(item);
item = new DefaultMenuItem("Redirect");
item.setIcon("ui-icon-search");
item.setCommand("#{menuBean.redirect}");
secondSubmenu.addElement(item);
model.addElement(secondSubmenu);
}
public MenuModel getModel() {
return model;
}
}
The menu is not working, as I try to run the project (I'm using Netbeans) I get the following error
Cannot find component with expression "messages" referenced from "j_idt5:j_idt6".
I'm not able to undertand why I get it, and Google has not been able to help me. Most doubts are about the annotation and the imports I used in MenuBean.java, since Netbeans tells me that annotations from "javax.faces.bean" will be deprecated in the next JSF version.
Another curious fact, in the XHTML page, if I change
<p:menu model="#{menuBean.model}" />
with <p:megaMenu model="#{menuBean.model}" />
I don't get the error, but the menu does not render the levle two menu items, I just see a menu with "Dynamic Submenu" and "Dynamic Actions", without any children entries.
What am I missing? I am willing to provide more information about my code/project, if necessary.
For me it works better to check the Primefaces showcase first, then later one can always refer to the manual.
According to the example "Programmatic menu" you need the part
<p:growl id="messages" showDetail="false"/>
I guess thats also in fact what the error message is stating, that its missing.
About the annotations see the recommendation i.e. here. Since JSF 2.2 it is recommended to move from JSF beans to CDI beans.