tags:

views:

1124

answers:

3

Is there a way to have the content of an html file inserted into a Facelet template? The Facelets tag will not work since it is only for including Facelet content.

To put it another way, I am looking for the Facelets equivalent to the JSP include directive (<%@ include file="..." %>).

+1  A: 

I may not understand what you need, but <ui:include> is not restricted to facelets content, you can insert valid xhtml with it, according to this link.

Consider following facelets file (test.jsp):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"&gt;

    <body>
        <f:view>
            <h:outputText value="Text outside include"/>
            <ui:include src="testinclude.html"/>
        </f:view>
    </body>
</html>

And following HTML file (testinclude.html):

<h2>Text from included page</h2>

It includes correctly the HTML content in the page. This also applies when using <ui:include> in a facelets template.

ckarmann
Thanks for pointing that out. After some experimenting, I found that including an html fragment such as this: foo <span>bar</span>generates an error: "Content is not allowed in prolog." And including fragment like this: <span>foo</span> <span>bar</span>generates an error: "The markup in the document following the root element must be well-formed." But this does work: <span>foo bar</span>So the included content *must* be well formed xml (but not necessarily valid xml).Am I correct in assuming that <ui:include> is the only insert mechanism in Facelets?
Tom
As far as I can tell, yes it is. Maybe you can try to use some other mechanisms like: - <jsp:include .../>, which I can't tell right now if it would work, but I doubt it.- There is also <a4j:include ...>, but RichFaces documentation seems to imply that it has the same behavior as <ui:include> - An HTML "iframe", but the inclusion would be done by the client side.- A custom JSF tag which would render the content of a file.
ckarmann
A: 

The only include mechanism in Facelets is , which doesn't allow arbitrary content to be included, only well formatted XML. There is no equivalent to the JSP include directive in Facelets.

Tom
A: 

This describes a solution to this: http://jdevelopment.nl/java/facelets-legacy-jsp/

The solution includes building a simple UI component that loads the JSP or Servlet content into a string and renders that via the normal response writer.

arjan