Search code examples
jsfprimefacestabviewuicomponents

HTML object as JSF UIComponent


I have a TabView whose tabs i generated with :

<p:tabView>
    <c:forEach var="app" items="#{data.apps}">
        <p:tab title="#{app.name}" closable="true" id="app_#{app.id}">
            <object data="#{app.startUri}" height="800" width="1150" />
        </p:tab>
    </c:forEach>
</p:tabView>

However, I had a problem with adding new tabs. I had to update the TabView which caused the tabs and their contents to reload which was a problem for my situation.

I then switched to create my TabView with a TabViewHelper bean.

But then there was a problem with adding the object tag as a child to the tab:

public TabView initializeTabView(List<App> apps) {
    tv = new TabView();
    Tab tab;
    for (App app : apps) {
        tab = new Tab();
        tab.setTitle(app.getName());
        HTMLObjectElementImpl content = new HTMLObjectElementImpl(owner, "test-object");
        tab.getChildren().add(content);
    tv.getChildren().add(tab);
    }
    return tv;
}

I get the following error:

The method add(UIComponent) in the type List is not applicable for the arguments (HTMLObjectElementImpl)

I can only add an UIComponent.

I'm not sure if HTMLObjectElementImpl is right for <object> but I did not find something better.

Is there an UIComponent for the HTML <object>-tag or is there another class available for HTML <object>?

Did anyone else have this problem and found a solution?


Solution

  • <object> is not a JSF component. It's basically considered as plain text.

    Simplest way would be to create a HtmlOutputText with escape set to false.

    HtmlOutputText html = new HtmlOutputText();
    html.setEscape(false);
    html.setValue("<object>...</object>");
    

    Beware of XSS attack holes in case you pass user-controlled variables to that.

    In future you try to create a JSF component programmatically, use a subclass of UIComponent and not some random JAXP implementation specific class. Or, better, fix that update problem so you can keep using XHTML code to create the view instead of verbose Java code.