public class UnmappedResourceHandler extends ResourceHandlerWrapper
/javax.faces.resource/* without the need for an additional FacesServlet prefix or suffix URL
pattern in the default produced resource URLs, such as /javax.faces.resource/faces/css/style.css or
/javax.faces.resource/css/style.css.xhtml. This resource handler will produce unmapped URLs like
/javax.faces.resource/css/style.css. This has the major advantage that the developer don't need the
#{resource} EL expression anymore in order to properly reference relative URLs to images in CSS files.
Given the following folder structure,
WebContent
`-- resources
`-- css
|-- images
| `-- background.png
`-- style.css
And the following CSS file reference (note: resource libraries are not supported by the
UnmappedResourceHandler! this is a technical limitation):
<h:outputStylesheet name="css/style.css" />
you can in css/style.css just use:
body {
background: url("images/background.png");
}
instead of
body {
background: url("#{resource['css/images/background.png']}");
}
To get it to run, this handler needs be registered as follows in faces-config.xml:
<application> <resource-handler>org.omnifaces.resourcehandler.UnmappedResourceHandler</resource-handler> </application>
And the FacesServlet needs to have an additional mapping /javax.faces.resource/* in
web.xml. For example, assuming that you've already a mapping on *.xhtml:
<servlet-mapping> <servlet-name>facesServlet</servlet-name> <url-pattern>*.xhtml</url-pattern> <url-pattern>/javax.faces.resource/*</url-pattern> </servlet-mapping>
If you're also using the CombinedResourceHandler or any other custom resource handler, then you need to
ensure that this is in faces-config.xml declared before the
UnmappedResourceHandler. Thus, like so:
<application> <resource-handler>org.omnifaces.resourcehandler.CombinedResourceHandler</resource-handler> <resource-handler>org.omnifaces.resourcehandler.UnmappedResourceHandler</resource-handler> </application>
Otherwise the combined resource handler will still produce mapped URLs. In essence, the one which is later registered wraps the previously registered one.
LOCALE_PREFIX, RESOURCE_EXCLUDES_DEFAULT_VALUE, RESOURCE_EXCLUDES_PARAM_NAME, RESOURCE_IDENTIFIER| Constructor and Description |
|---|
UnmappedResourceHandler(ResourceHandler wrapped)
Creates a new instance of this unmapped resource handler which wraps the given resource handler.
|
| Modifier and Type | Method and Description |
|---|---|
Resource |
createResource(String resourceName)
Delegate to
createResource(String, String, String) with null as library name and content
type. |
Resource |
createResource(String resourceName,
String libraryName)
Delegate to
createResource(String, String, String) with null as content type. |
Resource |
createResource(String resourceName,
String libraryName,
String contentType)
Delegate to
createResource(String, String, String) of the wrapped resource handler. |
ResourceHandler |
getWrapped() |
void |
handleResourceRequest(FacesContext context) |
boolean |
isResourceRequest(FacesContext context)
Returns
true if ExternalContext.getRequestServletPath() starts with value of
ResourceHandler.RESOURCE_IDENTIFIER. |
getRendererTypeForResourceName, libraryExistspublic UnmappedResourceHandler(ResourceHandler wrapped)
wrapped - The resource handler to be wrapped.public Resource createResource(String resourceName)
createResource(String, String, String) with null as library name and content
type.createResource in class ResourceHandlerWrapperpublic Resource createResource(String resourceName, String libraryName)
createResource(String, String, String) with null as content type.createResource in class ResourceHandlerWrapperpublic Resource createResource(String resourceName, String libraryName, String contentType)
createResource(String, String, String) of the wrapped resource handler. If it returns
non-null, then return a new instanceof UnmappedResource.createResource in class ResourceHandlerWrapperpublic boolean isResourceRequest(FacesContext context)
true if ExternalContext.getRequestServletPath() starts with value of
ResourceHandler.RESOURCE_IDENTIFIER.isResourceRequest in class ResourceHandlerWrapperpublic void handleResourceRequest(FacesContext context) throws IOException
handleResourceRequest in class ResourceHandlerWrapperIOExceptionpublic ResourceHandler getWrapped()
getWrapped in interface FacesWrapper<ResourceHandler>getWrapped in class ResourceHandlerWrapper