我正在尝试使用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">
想法?
答案 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); }
}
现在有效。