开发者

Decorate Renderer

I want to add a HTML5 attributes to simple text inputs. Therefore I'd like to decorate the standard Renderer for h:inputText. My solution is based on inheriting from com.sun.faces.renderkit.html_basic.TextRenderer which is specific to Mojarra. Is there a better way to decorate an existing Renderer without tying to a specific implementation and without having to re-implment functionality of the basic renderer?

Here is my solution so far:

@FacesRenderer(componentFamily=Html5InputRenderer.COMPONENT_FAMILY, rendererType=Html5InputRenderer.COMPONENT_TYPE)
public class Html5InputRenderer extends TextRenderer {

public static final String COMPONENT_FAMILY = "javax.faces.Input";

public static final String COMPONENT_TYPE = "javax.faces.Text";

public static final String RENDERED_TYPE_NAME = "renderedType";

@Override
public void encodeBegin(final FacesContext context, final UIComponent component) throws IOException {

    if (component.getAttributes().containsKey(RENDERED_TYPE_NAME)) {

        ResponseWriter writer = context.getResponseWriter();
        StringWriter buffer = new StringWriter();

        String replacement;

        try {
            ResponseWriter clonedWriter  = writer.cloneWithWriter(buffer);
            context.setResponseWriter(clonedWriter);
            s开发者_如何学运维uper.encodeBegin(context, component);

            String renderedMarkup = buffer.toString();

            // do some stuff with markup rendered by the basic renderer

            clonedWriter.close();

            if (null != replacement) {
                writer.write(replacement);
            }

        } finally {
            context.setResponseWriter(writer);
        }

    } else {
        super.encodeBegin(context, component);
    }
}

Thx, DS


You can decorate renderers by providing your own render kit. To do this, create a RenderKitFactory with a public constructor that takes an argument of type RenderKitFactory. Define the new RenderKitFactory in a faces-config.xml. During initialization, the JSF framework will pass the previously configured RenderKitFactory to your constructor.

See the JSF spec for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜