Friday, July 13, 2012

Programmatic Facelet:create Composite Component

There is no official API way to do this before JSF 2.2.
Suppose we have this composite component under webapproot/resources/test/component.xhtml
<!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:composite="http://java.sun.com/jsf/composite"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:iedit="http://java.sun.com/jsf/composite/iedit/"
    >
<h:head>
    <title>This content will not be displayed</title>
</h:head>
<h:body>
    <composite:interface>
        <composite:attribute name="tiptext" required="false" />
    </composite:interface>
    <composite:implementation>
        <h:panelGroup styleClass="propertylabelcell">
            <composite:renderFacet name="firstfacet" required="false"></composite:renderFacet>
        </h:panelGroup>
        A extra line of text
        <h:outputText value="#{cc.attrs.tiptext}"></h:outputText>
    </composite:implementation>
</h:body>
</html>

We can create one INSTANCE of this component programtically like this

FaceletFactory faceletFactory = (FaceletFactory) FactoryFinder
                .getFactory(FactoryFinder.FACELET_FACTORY);
Map<String, Object> attrs=new HashMap<String, Object>();
attrs.put("tiptext", "this is a test tip");
        UIComponent component = faceletFactory.createComponent(
                "http://java.sun.com/jsf/composite/" + library, name, attrs);

No comments:

Post a Comment