PrimeFaces 6.0 PanelMenu

时间:2016-08-08 10:52:21

标签: primefaces

我制作了一个PanelMenu,我试图在根子菜单中添加一个图标,但它不起作用。

<p:panelMenu>
    <p:submenu label="Üye işlemleri" icon="fa fa-user-plus">
        <p:menuitem value="Üye Kayıt" icon="fa fa-user-plus"
            action="#{redirect.toRegister()}" /> 
        <p:menuitem value="Üye Düzenle" icon="fa fa-pencil" />
    </p:submenu>
    <p:submenu label="Kullanıcı işlemleri" icon="ui-icon-extlink">
        <p:menuitem value="Anasayfa" icon="fa fa-home"
            action="#{redirect.toMainPage()}" />
        <p:menuitem value="Üye Kayıt" icon="fa fa-user-plus"
            action="#{redirect.toRegister()}" /> 
        <p:menuitem value="Üye Düzenle" icon="fa fa-pencil" />
    </p:submenu>
</p:panelMenu>

enter image description here

你知道吗?我也试过ui-icon(不是fontawesome)。我不想更改默认子菜单图标。我想在this page上添加默认图标旁边的内容。您可以在示例中看到,尤其是 Navigation&gt;链路。如您所见, Link 是一个子菜单。是否真的必须在2个子菜单​​中添加图标?

编辑------

我的渲染类得到扩展“PanelMenuRenderer”,我的faces-config.xml是;

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.PanelMenuRenderer</renderer-type>
        <renderer-class>denemeExample.my</renderer-class>
    </renderer>
</render-kit>

当我尝试运行它时,我收到错误。

An Error Occurred:

javax/servlet/jsp/jstl/core/Config
- Stack Trace

java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
    at com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:344)
    at com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:153)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

我试图解决这个问题,将jstl-1.2.jar和jsp-api.jar添加到WEB-INF / lib中但没有解决,服务器开始工作unlogical。它不能在xhtml中使用任何代码,如p:outputLabel或h:outputLabel。我认为tomcat不会运行。最后我使用PrimeFaces 6.0和jsf 2.2。所以你知道我该怎么做吗?

2 个答案:

答案 0 :(得分:1)

  

为了添加图标,它们是否真的必须是2个子菜单​​?

是的。如PrimeFaces Documentation中所述:

  

第一级子菜单呈现为手风琴面板,后代子菜单呈现为树节点。

如果查看source code,您会发现encodeRootSubmenu()中不会考虑根子菜单中的用户图标。另一方面,后代子菜单用户图标将在encodeDescendantSubmenu()中被考虑在内。

PanelMenus 中获取根子菜单的用户图标支持的一种可能解决方案是基于 PanelMenuRenderer 创建自定义渲染器并覆盖encodeRootSubmenu()。也许this question可以帮助你作为起点。

答案 1 :(得分:0)

此解决方案由irieill的帖子提供。实际上我的所作所为并不难,但每一种方式,我认为我应该发布它。

这是customPanelMenuRenderer(它的包是“com”)

public class customPanelMenuRenderer extends BaseMenuRenderer {

@Override
protected void encodeScript(FacesContext context, AbstractMenu abstractMenu) throws IOException {
    PanelMenu menu = (PanelMenu) abstractMenu;
    String clientId = menu.getClientId(context);
    WidgetBuilder wb = getWidgetBuilder(context);
    wb.initWithDomReady("PanelMenu", menu.resolveWidgetVar(), clientId).attr("stateful", menu.isStateful());
    wb.finish();
}

@Override
protected void encodeMarkup(FacesContext context, AbstractMenu abstractMenu) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    PanelMenu menu = (PanelMenu) abstractMenu;
    String clientId = menu.getClientId(context);
    String style = menu.getStyle();
    String styleClass = menu.getStyleClass();
    styleClass = styleClass == null ? PanelMenu.CONTAINER_CLASS : PanelMenu.CONTAINER_CLASS + " " + styleClass;

    writer.startElement("div", menu);
    writer.writeAttribute("id", clientId, "id");
    writer.writeAttribute("class", styleClass, "styleClass");
    if (style != null) {
        writer.writeAttribute("style", style, "style");
    }
    writer.writeAttribute("role", "menu", null);

    if (menu.getElementsCount() > 0) {
        List<MenuElement> elements = menu.getElements();

        for (MenuElement element : elements) {
            if (element.isRendered() && element instanceof Submenu) {
                encodeRootSubmenu(context, menu, (Submenu) element);
            }
        }
    }

    writer.endElement("div");
}

protected void encodeRootSubmenu(FacesContext context, PanelMenu menu, Submenu submenu) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String style = submenu.getStyle();
    String icon = submenu.getIcon();
    String styleClass = submenu.getStyleClass();
    styleClass = styleClass == null ? PanelMenu.PANEL_CLASS : PanelMenu.PANEL_CLASS + " " + styleClass;
    boolean expanded = submenu.isExpanded();
    String headerClass = expanded ? PanelMenu.ACTIVE_HEADER_CLASS : PanelMenu.INACTIVE_HEADER_CLASS;
    String headerIconClass = expanded ? PanelMenu.ACTIVE_TAB_HEADER_ICON_CLASS : PanelMenu.INACTIVE_TAB_HEADER_ICON_CLASS;
    System.out.println("headerIconClass = " + headerIconClass);
    System.out.println("icon = " + icon);
    boolean hasIcon = (icon != null);
    String contentClass = expanded ? PanelMenu.ACTIVE_ROOT_SUBMENU_CONTENT : PanelMenu.INACTIVE_ROOT_SUBMENU_CONTENT;

    //wrapper
    writer.startElement("div", null);
    writer.writeAttribute("class", styleClass, null);
    if (style != null) {
        writer.writeAttribute("style", style, null);
    }

    //header
    writer.startElement("h3", null);
    writer.writeAttribute("class", headerClass, null);
    writer.writeAttribute("role", "tab", null);
    writer.writeAttribute("tabindex", "0", null);

    //icon
    writer.startElement("span", null);
    writer.writeAttribute("class", headerIconClass, null);
    writer.endElement("span");

    //user icon
    if (hasIcon) {
        writer.startElement("span", null);
        writer.writeAttribute("class", "ui-icon " + icon, null);
        writer.endElement("span");
    }

    writer.startElement("a", null);
    writer.writeAttribute("href", "#", null);
    writer.writeAttribute("tabindex", "-1", null);
    writer.writeText(submenu.getLabel(), null);
    writer.endElement("a");

    writer.endElement("h3");

    //content
    writer.startElement("div", null);
    writer.writeAttribute("class", contentClass, null);
    writer.writeAttribute("role", "tabpanel", null);
    writer.writeAttribute("id", menu.getClientId(context) + "_" + submenu.getId(), null);
    writer.writeAttribute("tabindex", "0", null);

    if (submenu.getElementsCount() > 0) {
        List<MenuElement> elements = submenu.getElements();

        writer.startElement("ul", null);
        writer.writeAttribute("class", PanelMenu.LIST_CLASS, null);

        for (MenuElement element : elements) {
            if (element.isRendered()) {
                if (element instanceof MenuItem) {
                    MenuItem menuItem = (MenuItem) element;
                    String containerStyle = menuItem.getContainerStyle();
                    String containerStyleClass = menuItem.getContainerStyleClass();
                    containerStyleClass = (containerStyleClass == null) ? Menu.MENUITEM_CLASS : Menu.MENUITEM_CLASS + " " + containerStyleClass;

                    writer.startElement("li", null);
                    writer.writeAttribute("class", containerStyleClass, null);
                    if (containerStyle != null) {
                        writer.writeAttribute("style", containerStyle, null);
                    }
                    encodeMenuItem(context, menu, menuItem);
                    writer.endElement("li");
                } else if (element instanceof Submenu) {
                    encodeDescendantSubmenu(context, menu, (Submenu) element);
                }
            }
        }

        writer.endElement("ul");
    }

    writer.endElement("div");   //content

    writer.endElement("div");   //wrapper
}

protected void encodeDescendantSubmenu(FacesContext context, PanelMenu menu, Submenu submenu) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String icon = submenu.getIcon();
    String style = submenu.getStyle();
    String styleClass = submenu.getStyleClass();
    styleClass = styleClass == null ? PanelMenu.DESCENDANT_SUBMENU_CLASS : PanelMenu.DESCENDANT_SUBMENU_CLASS + " " + styleClass;
    boolean expanded = submenu.isExpanded();
    String toggleIconClass = expanded ? PanelMenu.DESCENDANT_SUBMENU_EXPANDED_ICON_CLASS : PanelMenu.DESCENDANT_SUBMENU_COLLAPSED_ICON_CLASS;
    String listClass = expanded ? PanelMenu.DESCENDANT_SUBMENU_EXPANDED_LIST_CLASS : PanelMenu.DESCENDANT_SUBMENU_COLLAPSED_LIST_CLASS;
    boolean hasIcon = (icon != null);
    String linkClass = (hasIcon) ? PanelMenu.MENUITEM_LINK_WITH_ICON_CLASS : PanelMenu.MENUITEM_LINK_CLASS;

    writer.startElement("li", null);
    writer.writeAttribute("id", submenu.getClientId(), null);
    writer.writeAttribute("class", styleClass, null);
    if (style != null) {
        writer.writeAttribute("style", style, null);
    }

    writer.startElement("a", null);
    writer.writeAttribute("class", linkClass, null);

    //toggle icon
    writer.startElement("span", null);
    writer.writeAttribute("class", toggleIconClass, null);
    writer.endElement("span");

    //user icon
    if (hasIcon) {
        writer.startElement("span", null);
        writer.writeAttribute("class", "ui-icon " + icon, null);
        writer.endElement("span");
    }

    //submenu label
    writer.startElement("span", null);
    writer.writeAttribute("class", PanelMenu.MENUITEM_TEXT_CLASS, null);
    writer.writeText(submenu.getLabel(), null);
    writer.endElement("span");

    writer.endElement("a");

    //submenu children
    if (submenu.getElementsCount() > 0) {
        List<MenuElement> elements = submenu.getElements();

        writer.startElement("ul", null);
        writer.writeAttribute("class", listClass, null);

        for (MenuElement element : elements) {
            if (element.isRendered()) {
                if (element instanceof MenuItem) {
                    writer.startElement("li", null);
                    writer.writeAttribute("class", Menu.MENUITEM_CLASS, null);
                    encodeMenuItem(context, menu, (MenuItem) element);
                    writer.endElement("li");
                } else if (element instanceof Submenu) {
                    encodeDescendantSubmenu(context, menu, (Submenu) element);
                }
            }
        }

        writer.endElement("ul");
    }

    writer.endElement("li");
}
}

这是faces-config.xml

  <render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.PanelMenuRenderer</renderer-type>
        <renderer-class>com.customPanelMenuRenderer</renderer-class>
    </renderer>
</render-kit>

使用这些代码,我们制作了一个自定义渲染器,然后注册到faces-config.xml。最后我们应该覆盖css代码,在我的情况下它会覆盖到这个类(它可能是你项目中的变化)

.ui-panelmenu .ui-panelmenu-header .ui-icon.fa{}

最后你有一个带有图标的rootSubmenu。