6

I'd like to know if it's possible to compose my own component (or call it Widget, Object).

I mean, instead of (for example) using h:panelGroup and a h:outputLabel inside it, make my own h:panelMarkzzz, as a composition of panelGroup and outputLabel.

Is it possible on JSF?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
markzzz
  • 47,390
  • 120
  • 299
  • 507

2 Answers2

7

Yes, it's possible to create a composition of existing components like that.

Kickoff example:

/resources/foo/group.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:cc="http://xmlns.jcp.org/jsf/composite">
    <cc:interface>
        <cc:attribute name="label" type="java.lang.String" required="true" />
    </cc:interface>
    <cc:implementation>
         <h:panelGroup>
             <h:outputLabel value="#{cc.attrs.label}" />
             <cc:insertChildren />
         </h:panelGroup>
    </cc:implementation>
</html>

/test.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://xmlns.jcp.org/jsf/html" 
    xmlns:foo="http://xmlns.jcp.org/jsf/composite/foo">
    <h:head>
        <title>Test</title>
    </h:head>
    <h:body>
        <foo:group label="Label value">
            <h:outputText value="This will appear after label inside the panelgroup" />
        </foo:group>
    </h:body>
</html>

The /foo folder name is free to your taste and you can reference it in XML namespace as http://xmlns.jcp.org/jsf/composite/XXX. The XHTML filename is the tag name.

That said, composite components have performance implications and they should only be used when the functional requirement is not achievable using a simple include or tagfile. In your specific case, you'd better use a tagfile instead. A composite component is only worthy when you actually need it for the <cc:interface componentType="...">.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

Perhaps you mean Composite Components?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Konrad Garus
  • 53,145
  • 43
  • 157
  • 230