2017-01-11 67 views
0

重要提示:如何設置P的的菜單項:從Bean類麪包屑

  • 有兩種方法
    1. crumBean(); =>此方法設置「p:menubar」primefaces組件
    2. bread(); =>此方法用於設置「p:breadcrumb」primefaces組件,我無法設置,這是我的實際問題。

我的問題: 如何設置primefaces從Bean類麪包屑菜單

我已經通過這些鏈接了,但是沒有找到有用的
Show Prime faces Bread Crumb dynamically
http://javabeat.net/primefaces-breadcrumb/

這裏是我的代碼:

Bean類

package test; 

    import java.util.ArrayList; 
    import java.util.List; 

    import javax.annotation.PostConstruct; 
    import org.primefaces.component.breadcrumb.BreadCrumb; 
    import org.primefaces.component.menuitem.UIMenuItem; 
    import org.primefaces.model.menu.DefaultMenuItem; 
    import org.primefaces.model.menu.DefaultMenuModel; 
    import org.primefaces.model.menu.DefaultSeparator; 
    import org.primefaces.model.menu.DefaultSubMenu; 
    import org.primefaces.model.menu.DynamicMenuModel; 
    import org.primefaces.model.menu.MenuElement; 
    import org.primefaces.model.menu.MenuModel; 
    import org.springframework.context.annotation.Scope; 
    import org.springframework.stereotype.Component;  

    @Component("breadBean") 
    @Scope(value = "view") 
    @SuppressWarnings("all") 
    public class BreadCrumBean { 

     BreadCrumb breadCrum = new BreadCrumb(); 
     MenuModel menuModel = new DynamicMenuModel(); 
     private MenuModel menubar = new DefaultMenuModel();  

     @PostConstruct 
     public void init(){ 
      try {    
       crumBean(); 
       bread(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
     FacesUtil.addErrorMessage(FacesUtil.getBundleKey(Constants.MESSAGE_RESOURCE, "general.generalError")); 
      }   
     }  
     public void crumBean(){ 

      DefaultSubMenu home = new DefaultSubMenu("Home"); 
      DefaultSubMenu home1 = new DefaultSubMenu("Home1"); 
      DefaultSubMenu index = new DefaultSubMenu("Index"); 

      DefaultMenuItem home2 = new DefaultMenuItem("Home2"); 
      DefaultMenuItem home3 = new DefaultMenuItem("Home3"); 

      DefaultMenuItem index1 = new DefaultMenuItem("Index1"); 

      DefaultMenuItem index2 = new DefaultMenuItem("Index2"); 
      index2.setAjax(false); 

      DefaultMenuItem index3 = new DefaultMenuItem("Index3"); 
      index3.setUrl("http://www.primefaces.org/showcase/"); 

      DefaultMenuItem index4 = new DefaultMenuItem("Index4"); 
      DefaultMenuItem index5 = new DefaultMenuItem("Index5"); 

      home1.addElement(index1); 
      home1.addElement(index2); 
      home1.addElement(index3); 

      index.addElement(index4); 
      index.addElement(index5); 
      index.addElement(new DefaultSeparator()); 
      index.addElement(index3); 

      home.addElement(home1); 
      home.addElement(home2); 
      home.addElement(new DefaultSeparator()); 
      home.addElement(home3); 

      this.menubar.addElement(home); 
      this.menubar.addElement(index); 
     }  
     public void bread(){ 

      DefaultSubMenu bread = new DefaultSubMenu("Bread"); 
      DefaultSubMenu bread1 = new DefaultSubMenu("Bread1"); 
      DefaultSubMenu crumb = new DefaultSubMenu("Crumb"); 

      DefaultMenuItem bread2 = new DefaultMenuItem("Bread2"); 
      DefaultMenuItem bread3 = new DefaultMenuItem("Bread3"); 

      DefaultMenuItem bread12 = new DefaultMenuItem("Bread12"); 

      DefaultMenuItem bread21 = new DefaultMenuItem("Bread21"); 
      bread21.setAjax(false); 

      DefaultMenuItem bread31 = new DefaultMenuItem("Bread31"); 
      bread31.setUrl("http://www.primefaces.org/showcase/"); 

      DefaultMenuItem crumb1 = new DefaultMenuItem("Crumb1"); 
      DefaultMenuItem crumb2 = new DefaultMenuItem("Crumb2"); 

      bread1.addElement(bread12); 
      bread1.addElement(bread21); 
      bread1.addElement(bread31); 

      crumb.addElement(crumb1); 
      crumb.addElement(crumb2); 
      crumb.addElement(new DefaultSeparator()); 
      crumb.addElement(bread31); 

      bread.addElement(bread1); 
      bread.addElement(bread2); 
      bread.addElement(new DefaultSeparator()); 
      bread.addElement(bread3); 

      this.menuModel.addElement(bread); 
      this.menuModel.addElement(crumb); 
      //How to set menuItem or menumodel in breadcrumb component 
     }  
     public BreadCrumb getBreadCrum() { 
      return breadCrum; 
     }  
     public void setBreadCrum(BreadCrumb breadCrum) { 
      this.breadCrum = breadCrum; 
     }  
     public MenuModel getMenuModel() { 
      return menuModel; 
     }  
     public void setMenuModel(MenuModel menuModel) { 
      this.menuModel = menuModel; 
     } 
     public MenuModel getMenubar() { 
      return menubar; 
     } 
     public void setMenubar(MenuModel menubar) { 
      this.menubar = menubar; 
     } 
    } 

test.xhtml

<!DOCTYPE html > 
    <html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:c="http://java.sun.com/jsp/jstl/core" 
     xmlns:fn="http://java.sun.com/jsp/jstl/functions" 
     xmlns:p="http://primefaces.org/ui"> 
    <h:head> 
    <style type="text/css"> 
       .bread-crum { 
        border: none; 
       } 
       .bread-crum a { 
        text-decoration: inherit; 
       } 
       .bread-crum .ui-state-disabled{ 
        opacity: inherit; 
        color:black; 
        font-weight:bold; 
        font-style: normal; 
       } 
    </style> 
    <script name="jquery/jquery.js" library="primefaces"></script> 
     <title>BreadCrumb</title> 
    </h:head> 
<h:body> 
    <h:form id="testForm"> 
        <p:menubar model="#{breadBean.menubar}" styleClass="bread-crum"></p:menubar>  
        <p:breadCrumb model="#{breadBean.menuModel}"> 
         <p:menuitem value="#{breadBean.menuModel}"> </p:menuitem> 
        </p:breadCrumb>  
      </h:form> 
     </h:body> 
     </html> 

回答

1

我已經通過這些鏈接了,但是沒有找到有用的

Show Prime faces Bread Crumb dynamically

如果你再看一次(第一個答案),你會發現你的問題幾乎是一樣的。 <p:breadCrumb/>組件的型號僅接受org.primefaces.model.menu.MenuItem的實例,因爲它是元素。

可能你沒有得到任何異常,因爲你有不同版本的PrimeFaces(空的麪包屑在6.0版本中呈現)。

注意:一旦你在<p:breadCrumb/>

調整的型號標籤 <p:menuitem/>被忽略