装饰渲染器

时间:2011-10-11 09:38:31

标签: jsf

我想将HTML5属性添加到简单的文本输入中。因此,我想为h:inputText装饰标准渲染器。我的解决方案基于继承自comjun.faces.renderkit.html_basic.TextRenderer,这是Mojarra特有的。有没有更好的方法来装饰现有的渲染器而不必绑定到特定的实现,而无需重新实现基本渲染器的功能?

到目前为止,这是我的解决方案:

@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);
            super.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

1 个答案:

答案 0 :(得分:0)

您可以按providing your own render kit装饰渲染器。为此,请使用带有RenderKitFactory类型参数的公共构造函数创建RenderKitFactory。在RenderKitFactory中定义新的faces-config.xml。在初始化期间,JSF框架会将先前配置的RenderKitFactory传递给您的构造函数。

有关详细信息,请参阅JSF spec