Wiki

Clone wiki

Oracle JSF Expert 1Z0-896 / Custom components

javax.faces.component.FacesComponent

Handle rendering itself

@FacesComponent("com.example.Spinner")
public class UISpinner extends UISpinner {

    @Override
    public void decode(FacesContext context) {
    }

    @Override
    public void encodeBegin(FacesContext context) {
    }

    @Override
    public void encodeChildren(FacesContext context) {
    }

    @Override
    public void encodeEnd(FacesContext context) {
    }

}

Delegate rendering to custom renderer

@FacesComponent("com.example.Spinner")
public class UISpinner extends UIInput {

    public UISpinner() {
       ...
       setRendererType("com.example.Spinner");
    }
}

javax.faces.render.FacesRenderer

@FacesRenderer(componentFamily="javax.faces.Input",
    rendererType="com.example.Spinner")
public class SpinnerRenderer extends Renderer {

    @Override
    public void decode(FacesContext facesContext, UIComponent component) {
    }

    @Override
    public void encodeBegin(FacesContext facesContext, UIComponent component) {
        ResponseWriter writer = facesContext.getResponseWriter();
        writer.startElement("input", component);
        writer.writeAttribute("x", "y", "z");
        writer.endElement("input");
    }

    @Override
    public void encodeChildren(FacesContext facesContext, UIComponent component) {
    }

    @Override
    public void encodeEnd(FacesContext facesContext, UIComponent component) {
    }
}

The Tag Library Descriptor

Name ends in taglib.xml eg. example.taglib.xml

Location

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/example.taglib.xml</param-value>
</context-param>

JAR Packaging taglib.xml in META-INF faces-config.xml in META-INF * Resources in META-INF/resources

Taglib syntax

<facelet-taglib ...>
    <namespace>http://example.com</namespace>
    <tag>
        <tag-name>spinner</tag-name>
        <component>
            <component-type>com.example.Spinner</component-type>
            <renderer-type>com.example.Spinner</renderer-type>
        </component>
    </tag>
</facelet-taglib>

Updated