- java.lang.Object
-
- jakarta.faces.application.ResourceHandler
-
- jakarta.faces.application.ResourceHandlerWrapper
-
- org.omnifaces.resourcehandler.DefaultResourceHandler
-
- org.omnifaces.resourcehandler.UnmappedResourceHandler
-
- All Implemented Interfaces:
FacesWrapper<ResourceHandler>
public class UnmappedResourceHandler extends DefaultResourceHandler
This
ResourceHandler
implementation allows the developer to map Faces resources on an URL pattern of/jakarta.faces.resource/*
(basically, the value ofResourceHandler.RESOURCE_IDENTIFIER
) without the need for an additionalFacesServlet
prefix or suffix URL pattern in the default produced resource URLs, such as/jakarta.faces.resource/faces/css/style.css
or/jakarta.faces.resource/css/style.css.xhtml
. This resource handler will produce unmapped URLs like/jakarta.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.So, given the following folder structure,
WebContent `-- resources `-- css |-- images | `-- background.png `-- style.css
And the following CSS file reference (note: the
library
is not supported by theUnmappedResourceHandler
! this is a technical limitation, just exclusively usename
):<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']}"); }
This has in turn the advantage that you don't need to modify the background image or font face URLs in CSS files from 3rd party libraries such as Twitter Bootstrap, FontAwesome, etcetera.
Installation
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/jakarta.faces.resource/*
inweb.xml
. You can just add it as a new<url-pattern>
entry to the existing mapping of theFacesServlet
. For example, assuming that you've already a mapping on*.xhtml
:<servlet-mapping> ... <url-pattern>*.xhtml</url-pattern> <url-pattern>/jakarta.faces.resource/*</url-pattern> </servlet-mapping>
CombinedResourceHandler
If you're also using the
CombinedResourceHandler
or any other custom resource handler, then you need to ensure that this is infaces-config.xml
declared before theUnmappedResourceHandler
. 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.
- Since:
- 1.4
- Author:
- Bauke Scholtz
- See Also:
RemappedResource
,DefaultResourceHandler
-
-
Field Summary
-
Fields inherited from class org.omnifaces.resourcehandler.DefaultResourceHandler
FACES_SCRIPT_RESOURCE_NAME, RES_NOT_FOUND
-
Fields inherited from class jakarta.faces.application.ResourceHandler
JSF_SCRIPT_LIBRARY_NAME, JSF_SCRIPT_RESOURCE_NAME, LOCALE_PREFIX, RESOURCE_CONTRACT_XML, RESOURCE_EXCLUDES_DEFAULT_VALUE, RESOURCE_EXCLUDES_PARAM_NAME, RESOURCE_IDENTIFIER, WEBAPP_CONTRACTS_DIRECTORY_PARAM_NAME, WEBAPP_RESOURCES_DIRECTORY_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description UnmappedResourceHandler(ResourceHandler wrapped)
Creates a new instance of this unmapped resource handler which wraps the given resource handler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Resource
decorateResource(Resource resource)
If the given resource is notnull
, then decorate it as an unmapped resource.void
handleResourceRequest(FacesContext context)
boolean
isResourceRequest(FacesContext context)
-
Methods inherited from class org.omnifaces.resourcehandler.DefaultResourceHandler
createResource, createResource, createResource, createResourceFromLibrary, decorateResource, getLibraryName
-
Methods inherited from class jakarta.faces.application.ResourceHandlerWrapper
createResourceFromId, createViewResource, getRendererTypeForResourceName, getViewResources, getViewResources, getWrapped, isResourceRendered, isResourceURL, libraryExists, markResourceRendered
-
-
-
-
Constructor Detail
-
UnmappedResourceHandler
public UnmappedResourceHandler(ResourceHandler wrapped)
Creates a new instance of this unmapped resource handler which wraps the given resource handler.- Parameters:
wrapped
- The resource handler to be wrapped.
-
-
Method Detail
-
decorateResource
public Resource decorateResource(Resource resource)
If the given resource is notnull
, then decorate it as an unmapped resource.- Overrides:
decorateResource
in classDefaultResourceHandler
- Parameters:
resource
- The resource to be decorated.- Returns:
- The decorated resource.
-
isResourceRequest
public boolean isResourceRequest(FacesContext context)
- Overrides:
isResourceRequest
in classResourceHandlerWrapper
-
handleResourceRequest
public void handleResourceRequest(FacesContext context) throws IOException
- Overrides:
handleResourceRequest
in classResourceHandlerWrapper
- Throws:
IOException
-
-