-
Available since OmniFaces 3.10
This
ResourceHandler
basically turns any concrete non-Facelets file into a Faces view, so that you can use EL expressions and even Faces components in them. The response content type will default to Faces.getMimeType(String)
which is configureable in web.xml
and overrideable via <f:view contentType="...">
.
Real world examples are /sitemap.xml
and /robots.txt
.
Installation
To get it to run, this handler needs be registered as follows in faces-config.xml
:
<application>
<resource-handler>org.omnifaces.resourcehandler.ViewResourceHandler</resource-handler>
</application>
To configure the Faces view resources, a "org.omnifaces.VIEW_RESOURCE_HANDLER_URIS" context parameter has to be provided wherein the view resources are specified as a comma separated string of context-relative URIs.
Here is an example configuration:
<context-param>
<param-name>org.omnifaces.VIEW_RESOURCE_HANDLER_URIS</param-name>
<param-value>/sitemap.xml, /products/sitemap.xml, /reviews/sitemap.xml, /robots.txt</param-value>
</context-param>
Wildcards in URIs are at the moment not supported.
The OmniViewHandler
will take care of rendering the view.
Demo
This resource handler is also configured with robots.txt and sitemap.xml on this showcase web application. You can find the demo source code below and here the generated robots.txt and sitemap.xml.
Demo
Demo source code
<f:view xmlns:f="jakarta.faces.core" contentType="text/plain">
sitemap:#{faces.requestBaseURL}sitemap.xml
</f:view>
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:f="jakarta.faces.core"
xmlns:ui="jakarta.faces.facelets"
xmlns:o="http://omnifaces.org/ui"
>
<f:view contentType="text/xml">
<o:sitemapUrl viewId="/whatsnew" />
<ui:repeat value="#{app.pages}" var="_page">
<o:sitemapUrl viewId="#{_page.viewId}" />
</ui:repeat>
</f:view>
</urlset>
Documentation & Sources