I'm creating a composite component in my application with following code :
<?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>
And am using this composite component in my index page as following:
<!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>
But whenever I'm trying to access index.xhtml page am getting following error.
javax.faces.component.UINamingContainer cannot be cast to org.primefaces.model.menu.MenuElement
But if i create composite component with following changes in composite:implementation part it's working fine.
composite component :
<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" />
Why am I getting javax.faces.component.UINamingContainer cannot be cast to org.primefaces.model.menu.MenuElement
while creating composite component? Any help would really useful. Thanks in advance.
Composite components are inherently of type UINamingContainer
. The <p:submenu>
supports only children of type MenuElement
.
Use a tagfile instead of a composite. Composites are generally only useful for composited input fields.
/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>
Usage:
<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>