org.omnifaces.resourcehandler
Class UnmappedResourceHandler

java.lang.Object
  extended by javax.faces.application.ResourceHandler
      extended by javax.faces.application.ResourceHandlerWrapper
          extended by org.omnifaces.resourcehandler.UnmappedResourceHandler
All Implemented Interfaces:
javax.faces.FacesWrapper<javax.faces.application.ResourceHandler>

public class UnmappedResourceHandler
extends javax.faces.application.ResourceHandlerWrapper

The UnmappedResourceHandler allows the developer to map JSF resources on an URL pattern of /javax.faces.resource/* without the need for an additional FacesServlet prefix or suffic 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
 

you can in css/style.css just use:

 body {
     background: url("images/background.png");
 }
 

instead of

 body {
     background: url("#{resource['css/images/background.png']}");
 }
 

Configuration

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>
 

CombinedResourceHandler

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.

Since:
1.4
Author:
Bauke Scholtz

Field Summary
 
Fields inherited from class javax.faces.application.ResourceHandler
LOCALE_PREFIX, RESOURCE_EXCLUDES_DEFAULT_VALUE, RESOURCE_EXCLUDES_PARAM_NAME, RESOURCE_IDENTIFIER
 
Constructor Summary
UnmappedResourceHandler(javax.faces.application.ResourceHandler wrapped)
          Creates a new instance of this unmapped resource handler which wraps the given resource handler.
 
Method Summary
 javax.faces.application.Resource createResource(java.lang.String resourceName, java.lang.String libraryName)
          Creates a new instance of a resource based on the given resource name and library name.
 javax.faces.application.ResourceHandler getWrapped()
           
 void handleResourceRequest(javax.faces.context.FacesContext context)
           
 boolean isResourceRequest(javax.faces.context.FacesContext context)
          Returns true if ExternalContext.getRequestServletPath() starts with value of ResourceHandler.RESOURCE_IDENTIFIER.
 
Methods inherited from class javax.faces.application.ResourceHandlerWrapper
createResource, createResource, getRendererTypeForResourceName, libraryExists
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnmappedResourceHandler

public UnmappedResourceHandler(javax.faces.application.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

createResource

public javax.faces.application.Resource createResource(java.lang.String resourceName,
                                                       java.lang.String libraryName)
Creates a new instance of a resource based on the given resource name and library name. If it is not null, then return a wrapped resource whose Resource.getRequestPath() returns the unmapped URL.

Overrides:
createResource in class javax.faces.application.ResourceHandlerWrapper

isResourceRequest

public boolean isResourceRequest(javax.faces.context.FacesContext context)
Returns true if ExternalContext.getRequestServletPath() starts with value of ResourceHandler.RESOURCE_IDENTIFIER.

Overrides:
isResourceRequest in class javax.faces.application.ResourceHandlerWrapper

handleResourceRequest

public void handleResourceRequest(javax.faces.context.FacesContext context)
                           throws java.io.IOException
Overrides:
handleResourceRequest in class javax.faces.application.ResourceHandlerWrapper
Throws:
java.io.IOException

getWrapped

public javax.faces.application.ResourceHandler getWrapped()
Specified by:
getWrapped in interface javax.faces.FacesWrapper<javax.faces.application.ResourceHandler>
Overrides:
getWrapped in class javax.faces.application.ResourceHandlerWrapper