3
votes

I was trying to create a composite component based on BalusC's excellent article. I couldn't get the attributes or any value from the backing component. 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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface componentType="test">
    <composite:attribute name="something" />
</composite:interface>

<composite:implementation>
    <h:outputText value="#{composite.attrs.something}" />
    <h:outputText value="#{composite.hello}" />
</composite:implementation>
</html>

I found out that the same code works then i use the "cc" namespace instead of "composite". 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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:cc="http://java.sun.com/jsf/composite">

<cc:interface componentType="test">
    <cc:attribute name="something" />
</cc:interface>

<cc:implementation>
    <h:outputText value="#{cc.attrs.something}" />
    <h:outputText value="#{cc.hello}" />
</cc:implementation>
</html>

The question is, why is it not working with the "composite" namespace? I couldn't find any information that "composite" is a JSF reserved word or something.

Thanks.

1

1 Answers

5
votes

You're confusing XML namespace with implicit EL object. The cc in #{cc} does not refer to the composite component XML namespace currently being used. It just references the composite component instance in the EL scope and is a fixed name. This construct works as good, with composite XML namespace.

<?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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface componentType="test">
    <composite:attribute name="something" />
</composite:interface>

<composite:implementation>
    <h:outputText value="#{cc.attrs.something}" />
    <h:outputText value="#{cc.hello}" />
</composite:implementation>
</html>

(note: I'd rather have used <ui:component> instead of <?xml?><!DOCTYPE><html>, exactly like as in the blog article which you linked there)

As to XML namespace, composite is unnecessarily long and does not fit nicely among all other abbreviated XML namespaces like h, f, c, etc. That's why many developers opt to use cc instead of composite as documented by JSF itself. Please note that the composite tag documentation itself also uses #{cc} throughout the examples with composite XML namespace.