如何建立动态菜单 [英] How to build a dynamic menu

查看:75
本文介绍了如何建立动态菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我刚开始使用jsf,所以我想构建一个动态菜单,其中包含从数据库加载的项目,但是我不确定我需要什么功能或如何为菜单构造后备bean.我已经有数据库创建和三个表Rol,User,Rol x User,Items.还有什么是项目表的好属性??

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

推荐答案

每个Rol应该具有Items,因此您需要一个额外的表ItemsXRol,我会为Item选择此列:id, label, url, tooltip, father_item, level当然,您应该选择列以满足您的需求.

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.

然后使用 Primefaces 创建菜单以动态地从数据库中获取项目,您可以使用MenuModel对象.

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

在.xhtml文件中,您应输入以下内容:

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

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

但是要生成从数据库获取值的菜单,您可以使用DAO从您的实体获取MenuItems.

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

如果菜单具有 n级格式,则可能很方便地生成MenuModel的递归方法,在该方法中,您必须遍历Items来创建第一级菜单,然后然后递归每个父亲的孩子.

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.

如果您想了解有关MenuModel的更多信息,请访问 https://www.primefaces.org/showcase/ui/menu/menu.xhtml

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

这篇关于如何建立动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆