Facebook social plugins and JSF

半城伤御伤魂 提交于 2020-01-01 20:04:43

问题


I have to integrate the Facebook social plugins into a JSF application. This recommends that I add the fbml namespace to the xhtml file that it's rendered in the response.

I have in my XHTML file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  ...
  xmlns:fb="http://www.facebook.com/2008/fbml"
  xmlns:og="http://ogp.me/ns#">

But the fb and og namespace won't be shown in the rendered source, only the XHTML namespace. How can I get these namespaces written to the response?

There is this issue: https://stackoverflow.com/questions/5199176/include-facebook-social-plugins-in-a-jsf2-page but it has not been answered yet.

The only idea I've got is to make an iframe and include a simple XHTML file (not a Facelet, just pure XHTML), but this seems to be dirty.

I hope someone has a better solution to that.


Additional information: I'm using facelets and seam 2.2.

I assume that ResponseWriter.startDocument() prints the Doctype and <html> element, is that correct? Or ist it just another UIComponent that renders the <html> element? It would be nice if I just could implement a custom ResponseWriter and override startDocument() and set my custom writer as default.

This leads me to 2 questions:

  1. Which class should I override so I don't have to implement every method of the abstract ResponseWriter?
  2. How would I tell my application to use my custom ResponseWriter?

Or does implementing a custom component that renders the <html> tag the job? I'm asking this because facelets seems to render the <html> Tag by itself and there seems to be no way to change this, that's why I came up with overriding the ResponseWriter.


回答1:


I found out that I just had to write a custom component:

public class CvHTML extends UIOutput {
    @Override
    public void encodeBegin(final FacesContext context) throws IOException {
        final ResponseWriter writer = context.getResponseWriter();
        writer.startDocument();
        writer.startElement("HTML", null);
        writer.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml", null);
        writer.writeAttribute("xmlns:fb", "http://www.facebook.com/2008/fbml", null);
        writer.writeAttribute("xmlns:og", "http://ogp.me/ns#", null);
    }

    @Override
    public void encodeEnd(final FacesContext context) throws IOException {
        final ResponseWriter writer = context.getResponseWriter();
        writer.endElement("HTML");
        writer.endDocument();
    }
}

and call now in the main template:

<cv:html xmlns="http://www.w3.org/1999/xhtml" lang="en"
    xmlns:s="http://jboss.com/products/seam/taglib"
    ....
    xmlns:cv="http://your.name.space/foo">


来源:https://stackoverflow.com/questions/7162017/facebook-social-plugins-and-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!