title: Primefaces Programmatic Menu
body: <p>this is my xhtml</p>
  
  <pre><code>        &lt;h:form&gt; 
          &lt;p:growl id="messages" showDetail="false"/&gt;
            &lt;p:menubar&gt;
                      &lt;p:submenu label="Report" icon="ui-icon-arrowreturnthick-1-e"&gt;
                          &lt;p:submenu label="User Management" icon="ui-icon-arrowreturnthick-1-e"&gt;
                              &lt;p:menuitem value="Full Report" url="/secured/report/reportList.xhtml" icon="ui-icon-arrowreturnthick-1-e" /&gt;
                              &lt;p:menuitem value="Generate Report" url="/secured/report/generateReport.xhtml" icon="ui-icon-arrowreturnthick-1-e"  /&gt;
                          &lt;/p:submenu&gt;
                          &lt;p:submenu label="Service Management" icon="ui-icon-arrowreturnthick-1-e"&gt;
                              &lt;p:menuitem value="Full Report" url="/secured/report/reportList.xhtml" icon="ui-icon-arrowreturnthick-1-e" /&gt;
                              &lt;p:menuitem value="Generate Report" url="/secured/report/generateReport.xhtml" icon="ui-icon-arrowreturnthick-1-e"  /&gt;
                          &lt;/p:submenu&gt;
                      &lt;/p:submenu&gt;
               &lt;p:menu  model="#{menuModelBean.model}"&gt;
               &lt;/p:menu&gt;
            &lt;/p:menubar&gt;
        &lt;/h:form&gt;
  </code></pre>
  
  <p>this my bean</p>
  
  <pre><code>    package com.bat.qpa.beans;
  
      import javax.annotation.PostConstruct;
      import javax.faces.application.FacesMessage;
      import javax.faces.bean.ManagedBean;
      import javax.faces.context.FacesContext;
      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(name = "menuModelBean")
      public class MenuModelBean {
  
  private MenuModel model = new DefaultMenuModel();
  
  @PostConstruct
  public void init() {
      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("#{menuView.save}");
      item.setUpdate("messages");
      secondSubmenu.addElement(item);
  
      item = new DefaultMenuItem("Delete");
      item.setIcon("ui-icon-close");
      item.setCommand("#{menuView.delete}");
      item.setAjax(false);
      secondSubmenu.addElement(item);
  
      item = new DefaultMenuItem("Redirect");
      item.setIcon("ui-icon-search");
      item.setCommand("#{menuView.redirect}");
      secondSubmenu.addElement(item);
  
      model.addElement(secondSubmenu);
  }
  
  public MenuModel getModel() {
      return model;
  }
  
  public void setModel(MenuModel model) {
      this.model = model;
  }
  
  
  public String openAction() {
      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Open action has activiated asynchrounsly !"));
      return "";
  }
  </code></pre>
  
  <p>}</p>
  
  <p>this is the error. I am using primeface 5.1.</p>
  
  <pre><code>   org.primefaces.component.menu.Menu cannot be cast to 
     org.primefaces.model.menu.MenuElement
  </code></pre>
  
  <p>I want to create a   Programmatic Menu using managed bean.First part is my xhtml page, second part is Managed Bean and the third part is error.I have searched,But i couldnt find find any error like this.
  I am following the way in Primefaces sites demo. But getting the error. Why?</p>