1

我正在尝试渲染具有属性的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">

想法?

4

1 回答 1

3

好吧,看来我应该打电话super.encodeBegin(context, component)。(感谢 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); }

}

现在可以了。

于 2013-06-09T10:15:17.323 回答