2

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="..." %>.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Tom
  • 371
  • 3
  • 8

4 Answers4

4

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">
<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">

    <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.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • 1
    Thanks for pointing that out. After some experimenting, I found that including an html fragment such as this: foo bar generates an error: "Content is not allowed in prolog." And including fragment like this: foo bar generates an error: "The markup in the document following the root element must be well-formed." But this does work: foo bar So the included content *must* be well formed xml (but not necessarily valid xml). Am I correct in assuming that is the only insert mechanism in Facelets? – Tom Sep 29 '09 at 21:05
  • As far as I can tell, yes it is. Maybe you can try to use some other mechanisms like: - , which I can't tell right now if it would work, but I doubt it. - There is also , but RichFaces documentation seems to imply that it has the same behavior as - 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. – Cyrille Ka Oct 02 '09 at 09:27
1

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
  • 371
  • 3
  • 8
1

This describes a solution to this: http://arjan-tijms.omnifaces.org/2010/04/facelets-and-legacy-jsp.html

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 Tijms
  • 37,782
  • 12
  • 108
  • 140
1

Omnifaces's <o:resourceInclude> can be used to include arbitrary content directly to the response. Which means it doesn't have to be well formed xml as with <ui:include>. Also you can include content in <h:head> section of your JSF page, which is tough to achieve otherwise.

http://showcase.omnifaces.org/components/resourceInclude

JanM
  • 1,385
  • 1
  • 15
  • 25