javax.faces.component.UINamingContainer 无法转换为 org.primefaces.model.menu.MenuElement

javax.faces.component.UINamingContainer cannot be cast to org.primefaces.model.menu.MenuElement

我正在使用以下代码在我的应用程序中创建一个复合组件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui"
      xmlns:composite="http://xmlns.jcp.org/jsf/composite">

    <composite:interface>

        <composite:attribute name="url" required="true" type="java.lang.String" />
        <composite:attribute name="label" required="true" type="java.lang.String" />
        <composite:attribute name="compId" required="true" type="java.lang.String" />

    </composite:interface>

    <composite:implementation>
        <p:menuitem  id="#{cc.attrs.compId}" value="#{cc.attrs.label}"  url="#{cc.attrs.url}"  />
    </composite:implementation>

</html>

我在我的索引页面中使用这个复合组件如下:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ex="http://xmlns.jcp.org/jsf/composite/example">

        <h:head>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
                <title>PrimeFaces</title>
            </f:facet>
        </h:head>

        <h:body>
            <p:menu>
                <p:submenu id="admin" label="Admin" >
                    <ex:menuItem label="Go" url="www.google.com" compId="tempMenu"/>
                </p:submenu>
            </p:menu>
        </h:body>

</html>

但每当我尝试访问 index.xhtml 页面时,都会出现以下错误。

 javax.faces.component.UINamingContainer cannot be cast to org.primefaces.model.menu.MenuElement

但是如果我创建复合组件并在 composite:implementation 部分进行以下更改,它工作正常。

复合组件:

 <p:menu>
        <p:submenu id="admin" label="Admin" >
            <p:menuitem  value="#{cc.attrs.label}"  url="#{cc.attrs.url}"  id="#{cc.attrs.compId}"/>
        </p:submenu>
    </p:menu>

index.xhtml:

<ex:menuItem label="Go" url="www.google.com" compId="tempMenu" />

为什么我在创建复合组件时得到 javax.faces.component.UINamingContainer cannot be cast to org.primefaces.model.menu.MenuElement?任何帮助都会非常有用。提前致谢。

复合组件本质上属于 UINamingContainer 类型。 <p:submenu> 仅支持 MenuElement.

类型的子项

使用标记文件而不是复合文件。复合通常只对复合输入字段有用。

/WEB-INF/tags/menuItem.xhtml:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui"
>
    <p:menuitem id="#{id}" value="#{label}" url="#{url}" />
</ui:composition>

/WEB-INF/my.taglib.xml:

<facelet-taglib
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd"
    version="2.2"
>
    <namespace>http://www.example.com/ui</namespace>

    <tag>
        <tag-name>menuItem</tag-name>
        <source>tags/menuItem.xhtml</source>
        <attribute>
            <description>Component ID.</description>
            <name>id</name>
            <required>true</required>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <description>Menu item label.</description>
            <name>label</name>
            <required>true</required>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <description>Menu item URL.</description>
            <name>url</name>
            <required>true</required>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</facelet-taglib>

/WEB-INF/web.xml:

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>

用法:

<html ... xmlns:ex="http://example.com/ui">
...
<p:menu>
    <p:submenu id="admin" label="Admin" >
        <ex:menuItem id="tempMenu" label="Go" url="www.google.com" />
    </p:submenu>
</p:menu>

另请参阅:

  • How to create a composite component for a datatable column?
  • When to use <ui:include>, tag files, composite components and/or custom components?