自定义头部渲染器属性未通过

时间:2013-06-09 10:07:19

标签: java jsf jsf-2

我正在尝试使用head属性呈现prefix元素。为了在JSF中执行此操作:

<h:head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# XXXX: http://ogp.me/ns/fb/XXXX#">

我创建了一个自定义head渲染器:

public class FacebookHeadRenderer extends HeadRenderer{
    @Override
    public void encodeBegin(FacesContext context, UIComponent component)
            throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("head", component);
        RenderKitUtils.renderPassThruAttributes(context,
                                                writer,
                                                component,
                                                HEAD_ATTRIBUTES);
        System.out.println("FacebookHeadRenderer!");
    }

    private static final Attribute[] HEAD_ATTRIBUTES;
    static {
        Attribute[] baseAttributes = AttributeManager.getAttributes(
                AttributeManager.Key.OUTPUTHEAD);
        HEAD_ATTRIBUTES = new Attribute[baseAttributes.length + 1];
        for (int i = 0; i < baseAttributes.length; ++i) {
            HEAD_ATTRIBUTES[i] = baseAttributes[i];
        }
        HEAD_ATTRIBUTES[HEAD_ATTRIBUTES.length - 1] = Attribute.attr("prefix");
    }
}

并在faces-config.xml注册:

<render-kit>
    <renderer>
        <component-family>javax.faces.Output</component-family>
        <renderer-type>javax.faces.Head</renderer-type>
        <renderer-class>com.localgp.jsf.FacebookHeadRenderer</renderer-class>
    </renderer>
</render-kit>
正在调用

FacebookRenderer(正在打印FacebookHeadRenderer!进行验证),但未呈现prefix属性。

正如完整性检查一样,我添加了lang属性:

<h:head lang="en_AU"
        prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# XXXX: http://ogp.me/ns/fb/XXXX#">

我得到的只是

<head lang="en_AU">

想法?

1 个答案:

答案 0 :(得分:3)

好吧,我好supposed to call super.encodeBegin(context, component) first。 (感谢BalusC。)

所以我修改了渲染器:

public class FacebookHeadRenderer extends HeadRenderer {
    private static final Attribute[] EXTRA_HEAD_ATTRIBUTES =
            { Attribute.attr("prefix") };

    @Override
    public void encodeBegin(FacesContext context, UIComponent component)
            throws IOException {
        super.encodeBegin(context, component);
        ResponseWriter writer = context.getResponseWriter();
        RenderKitUtils.renderPassThruAttributes(context,
                                                writer,
                                                component,
                                                EXTRA_HEAD_ATTRIBUTES); }

}

现在有效。