【问题标题】:how to render panel menu items dynamically in jsf2 (richfaces)如何在jsf2(richfaces)中动态呈现面板菜单项
【发布时间】:2014-07-21 04:22:49
【问题描述】:

我是 JSF 2.0 的新手,并试图将面板菜单项动态呈现到我的 xhtml 页面中。 例如:

Header_1
 |---SubHeader_1
 |     |----Item_1.1
 |     |----Item_1.2
 |     |----Item_1.3
 |---SubHeader_2
 |     |----Item_2.1
 |     |----Item_2.2

我想从数据库中动态加载Item_1.xItem_2.x 到我的home.xhtml 中。请提供您的宝贵建议,我该怎么做。

【问题讨论】:

    标签: jsf-2 richfaces


    【解决方案1】:

    你可以试试下面的代码:希望对你有帮助-

    菜单项的数据由 MenuItem 类表示

    public class MenuItem {
        private int id;
        private String label;
    
        public MenuItem(String label, int id) {
           super();
           this.label = label;
           this.id = id;
        }
    
        public int getId() {
           return this.id;
        }
    
        public String getLabel() {
           return this.label;
        }
    
        public void setId(int id) {
           this.id = id;
        }
    
        public void setLabel(String label) {
           this.label = label;
        }
    
        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("MenuItem [id=");
            builder.append(this.id);
            builder.append(", label=");
            builder.append(this.label);
            builder.append("]");
            return builder.toString();
        }
     }
    

    DynamicMenu 类支持动态菜单。它提供了菜单项列表和一个操作方法:

    import java.util.ArrayList;
    import java.util.List;
    import org.apache.log4j.Logger;
    import org.jboss.seam.annotations.Name;
    
    @Name("dynMenu")
    public class DynamicMenu {
    
        private Logger log = Logger.getLogger(DynamicMenu.class.getName());
    
        public void action(int id) {
            log.info("Action called with menu item id: " + id);
        }
    
        public List<menuitem> getMenuItems() {
    
            List<menuitem> menuItems = new ArrayList<menuitem>();
    
            menuItems.add(new MenuItem("Menu Item #1", 1));
            menuItems.add(new MenuItem("Menu Item #2", 2));
            menuItems.add(new MenuItem("Menu Item #3", 3));
    
            return menuItems;
        }
    }
    

    以下代码 sn -p 包含动态菜单 xhtml 示例。动态菜单项中的关键是&lt;c:forEach&gt; 迭代器。命名空间声明很重要,应该是xmlns:c="http://java.sun.com/jstl/core"。如果你使用xmlns:c="http://java.sun.com/jsp/jstl/core"命名空间,迭代器将不起作用!

    <h:form xmlns:h="http://java.sun.com/jsf/html">
        <rich:dropDownMenu value="Dynamic Menu Item Example" style="text-decoration:none;">
            <c:forEach xmlns:c="http://java.sun.com/jstl/core" var="item" 
                                            items="#{dynMenu.getMenuItems()}">
    
                <rich:menuItem id="menuItem#{item.id}" submitMode="ajax"
                        value="#{item.label}" action="#{dynMenu.action(item.id)}">
                </rich:menuItem>
            </c:forEach>
        </rich:dropDownMenu>
    </h:form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-03
      • 1970-01-01
      • 2011-08-24
      • 2012-07-29
      相关资源
      最近更新 更多