简体   繁体   English

如何从bean JSF填充菜单项

[英]How to populate menu items from bean JSF

I am coding a dropdown menu, where a button menu is clicked and it will show it's submenus or menuitems. 我正在编码一个下拉菜单,单击一个按钮菜单,它将显示它的子菜单或菜单项。 Currently, I am having trouble to populate the p:menuitem from the bean. 目前,我很难从bean中填充p:menuitem。 Previously, I tried to code as such 以前,我尝试这样编码

<p:menu label="PrimeFaces" icon="ui-icon-heart">
  <p:menuitem value="Home" url="http://www.primefaces.org" icon="ui-icon-home" />
  <p:menuitem value="Docs" url="http://www.primefaces.org/documentation" icon="ui-icon-document" />
  <p:menuitem value="Download" url="http://www.primefaces.org/downloads" icon="ui-icon-arrowthick-1-s" />
  <p:menuitem value="Support" url="http://www.primefaces.org/support" icon="ui-icon-wrench" />
</p:menu>

As you noticed, the menuitem values (Home, Docs, etc) are hardcoded in the JSF. 正如您所注意到的,菜单项值(Home,Docs等)在JSF中被硬编码。 How can I fetch the values from bean and populate as the menu item ? 如何从bean中获取值并填充为菜单项? Does p:menuitem have something similar like the f:selectItems when I can set a list as the value? 当我可以将列表设置为值时,p:menuitem是否具有类似于f:selectItems的东西?

What I want to try to achieve is to populate menu items from the bean and a method will be fired when the individual menu item is selected. 我想要尝试实现的是从bean中填充菜单项,并且在选择单个菜单项时将触发一种方法。

Help is much appreciated. 非常感谢您的帮助。 Thank you. 谢谢。

Are you sure the menu is a jsf component? 您确定菜单是jsf组件吗? I think you may be using primefaces component, The name scheme is usually defined as follows and p:menu is a component of primefaces: 我认为您可能正在使用primefaces组件,名称方案通常定义如下,而p:menu是primefaces的组件:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:sec="http://www.springframework.org/security/tags" >

You can customize and use the menu from the bean as follows: 您可以自定义并使用Bean中的菜单,如下所示:

xhtml: xhtml:

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

Bean: 豆角,扁豆:

public class MenuBean {

    private MenuModel model;

    public MenuBean() {

    model = new DefaultMenuModel();
    //First submenuDefaultSubMenu 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 submenuDefaultSubMenu secondSubmenu = new DefaultSubMenu("Dynamic Actions");
    item = new DefaultMenuItem("Save");
    item.setIcon("ui-icon-disk");
    item.setCommand("#{menuBean.save}");
    item.setUpdate("messages");
    secondSubmenu.addElement(item);
    item = new DefaultMenuItem("Delete");
    item.setIcon("ui-icon-close");
    item.setCommand("#{menuBean.delete}");
    item.setAjax(false);
    secondSubmenu.addElement(item);
    item = new DefaultMenuItem("Redirect");
    item.setIcon("ui-icon-search");
    item.setCommand("#{menuBean.redirect}");
    secondSubmenu.addElement(item);
    model.addElement(secondSubmenu);
    }

    public MenuModel getModel() { return model; }
}

Here you can see all the documentation: https://www.primefaces.org/docs/guide/primefaces_user_guide_6_2.pdf (pag. 333) 在这里您可以看到所有文档: https ://www.primefaces.org/docs/guide/primefaces_user_guide_6_2.pdf(第333页)

If you use Primefaces you can try: 如果您使用Primefaces,则可以尝试:

bean: 豆角,扁豆:

public class MyItem {
  private String value;
  private String url;
  private String icon;
  ....
  getters/setters
  ...
}


public List<MyItem> getMenuItems() {
  // build list
}

xhtml: xhtml:

<p:repeat value="#{bean.menuItems}" var="item">
  <p:menuitem value="#{item.value}" url="#{item.url}" icon="#{item.icon}" />
</prepeat>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM