【问题标题】:javax.faces.component.UINamingContainer cannot be cast to org.primefaces.model.menu.MenuElementjavax.faces.component.UINamingContainer 不能转换为 org.primefaces.model.menu.MenuElement
【发布时间】:2016-05-01 14:55:04
【问题描述】:

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

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

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

复合组件:

 <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?任何帮助都会非常有用。提前致谢。

【问题讨论】:

    标签: jsf primefaces composite-component


    【解决方案1】:

    复合组件本质上属于UINamingContainer 类型。 &lt;p:submenu&gt; 仅支持 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>
    

    另见:

    【讨论】:

    • 感谢@BalusC。我得到了明确的信息。非常感谢。
    • @Roudhran 那么请为他的回答投票,请记住这不是论坛。这里 p:menuButton 中的先前 p:link 导致了此异常。
    • @Roland:投票至少需要 15 个声望。
    • @BalusC 是的,对。希望他不要忘记。 :-) 我有时会发布这个来记住 stackoverflow 是什么,因为有些人没有抓住重点。
    猜你喜欢
    • 2023-03-06
    • 2018-03-21
    • 2018-02-10
    • 2023-03-22
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多