How to build a dynamic menu

本小妞迷上赌 提交于 2021-01-27 18:09:27

问题


Hey I new using jsf so I want to build a dynamic menu with items loaded from database but I'm not sure what functions do I need or how to construct the backing bean for the menu. I already have the database create and three tables Rol, User, Rol x User, Items. Also what will be good attributes for the item table??.


回答1:


Each Rol should have Items so you need an extra table ItemsXRol i would choose this columns for Item : id, label, url, tooltip, father_item, level something like that, of course you should choose your columns in order to fill your needs.

Then to create the menu dynamically getting the items from the db using Primefaces you can use a MenuModel object.

//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
    ...

And in the .xhtml file you put something like this:

<p:menu model="#{menuBean.model}"/> 

But to generate the menu getting values from the database you can use a DAO to get the MenuItems from your entity.

If your menu has a n-level format it could become handy a recursive method to generate the MenuModel in which you have to iterate over your Items to create the 1st level menus and then recursively the childs of each father.

If you want to read more about MenuModel go to https://www.primefaces.org/showcase/ui/menu/menu.xhtml




回答2:


Primefaces provide you a programmatic way to dynamically build your menu using org.primefaces.model.menu.* classes. Use a MenuModel in your managed bean and reference it on your facelet page. Example:

public class MenuBean {  

    private MenuModel model;  

    public MenuBean() {
        // Build your menu here
    }

    // Gets and sets
}

And on your facelet:

<p:menu model="#{menuBean.model}"/>

Further details: http://www.primefaces.org/showcase/ui/menu.jsf



来源:https://stackoverflow.com/questions/19659087/how-to-build-a-dynamic-menu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!