i need to create primefaces bread crumb menu dynamically in managed bean, i found this How to make a primefaces breadcrumb dynamic? example of dynamic menu creation on stack overflow and primefaces. following is my code to create a dynamic menu
public class BreadCrumb {
private MenuModel model;
public BreadCrumb() {
createMenu();
}
@PostConstruct
private void createMenu()
{
model = new DefaultMenuModel();
// First submenu
DefaultSubMenu firstSubmenu = new DefaultSubMenu("First Sub Menu");
DefaultMenuItem item = new DefaultMenuItem("Item1");
item.setUrl("http://www.primefaces.org");
item.setIcon("ui-icon-disk");
firstSubmenu.addElement(item);
model.addElement(firstSubmenu);
// Second submenu
DefaultSubMenu secondSubmenu = new DefaultSubMenu("Second Sub Menu");
item = new DefaultMenuItem("Item2");
item.setIcon("ui-icon-disk");
item.setUrl("http://www.primefaces.org");
secondSubmenu.addElement(item);
//third sub menu
item = new DefaultMenuItem("Item3");
item.setIcon("ui-icon-close");
secondSubmenu.addElement(item);
//forth sub menu
item = new DefaultMenuItem("Item4");
item.setIcon("ui-icon-search");
secondSubmenu.addElement(item);
model.addElement(secondSubmenu);
}
public MenuModel getModel() {
return model;
}
}
and my xhtml is
<h:form>
<p:breadCrumb model="#{breadCrumbBean.model}">
</p:breadCrumb>
</h:form>
i tried using model with mega menu, menu and menu item but sometimes i doesn't render anything and sometimes it gives an exception. for current scenario i am getting this exception.
Error Rendering View[/revdep/breadcrumb.xhtml]: java.lang.ClassCastException: org.primefaces.model.menu.DefaultSubMenu cannot be cast to org.primefaces.model.menu.MenuItem
at org.primefaces.component.breadcrumb.BreadCrumbRenderer.encodeMarkup(BreadCrumbRenderer.java:42) [primefaces-5.2.jar:5.2]
at org.primefaces.component.menu.BaseMenuRenderer.encodeEnd(BaseMenuRenderer.java:113) [primefaces-5.2.jar:5.2]
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final]
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final]
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:100) [primefaces-5.2.jar:5.2]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at com.googlecode.webutilities.filters.CompressionFilter.doFilter(CompressionFilter.java:112) [webutilities-0.0.5.jar:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113) [spring-orm-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at pk.gov.fbr.insys.base.IRISFilterBean.doFilter(IRISFilterBean.java:105) [classes:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:489) [jbossweb-7.0.10.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:154) [jboss-as-web-7.1.0.Final.jar:7.1.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.10.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.10.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.10.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.10.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.10.Final.jar:]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_33]
i can't seem to find any solution for creating dynamic breadcrumb, any help would be appreciated
As Joffrey Hernandez says, "Of course, you have to adapt this one for BreadCrumb...".
The stacktrace:
is telling you that you can't use the class
org.primefaces.model.menu.DefaultSubMenu
in a<p:breadcrumb>
.Furthermore, a breadcrumb is an one-dimensional structure, so you can't nest elements.
Therefore, the solution is to use an implementation of
org.primefaces.model.menu.MenuItem
, likeorg.primefaces.model.menu.DefaultMenuItem
instead oforg.primefaces.model.menu.DefaultSubMenu
.For example: