public class CombinedResourceHandler extends ResourceHandlerWrapper implements SystemEventListener
ResourceHandler
implementation will remove all separate script and stylesheet resources which have the
target
attribute set to "head"
from the UIViewRoot
and create a combined one
for all scripts and another combined one for all stylesheets.
To get it to run, this handler needs be registered as follows in faces-config.xml:
<application> <resource-handler>org.omnifaces.resourcehandler.CombinedResourceHandler</resource-handler> </application>
Note that the target
attribute of <h:outputStylesheet>
already defaults to
"head"
but the one of <h:outputScript>
not. So if you have placed this inside the
<h:head>
, then you would still need to explicitly set its target
attribute to
"head"
, otherwise it will be treated as an inline script and not be combined. This is a design
limitation.
<h:head> <h:outputStylesheet name="style.css" /> <h:outputScript name="script.js" target="head" /> </h:head>
If you want them to appear after any auto-included resources, then move the declarations to the
<h:body>
.
<h:body> <h:outputStylesheet name="style.css" /> <h:outputScript name="script.js" target="head" /> </h:body>
The following context parameters are available:
"org.omnifaces.COMBINED_RESOURCE_HANDLER_EXCLUDED_RESOURCES"
|
Comma separated string of resource identifiers of <h:head> resources which needs to be excluded
from combining. For example:
<param-value>primefaces:primefaces.css, javax.faces:jsf.js</param-value> Any combined resource will be included after any of those excluded resources. |
"org.omnifaces.COMBINED_RESOURCE_HANDLER_SUPPRESSED_RESOURCES"
|
Comma separated string of resource identifiers of <h:head> resources which needs to be suppressed
and removed. For example:
<param-value>skinning.ecss, primefaces:jquery/jquery.js</param-value>
|
Here, the "resource identifier" is the unique combination of library name and resource name, separated by a colon,
exactly the syntax as you would use in #{resource}
in EL. If there is no library name, then just omit
the colon. Valid examples of resource identifiers are filename.ext, folder/filename.ext,
library:filename.ext and library:folder/filename.ext.
Note that this combined resource handler is not able to combine resources which are not
been added as a component resource, but are been hardcoded in some renderer (such as theme.css
in case
of PrimeFaces and several JavaScript files in case of RichFaces), or are been definied using plain HTML
<link>
or <script>
elements. Also, when you're using RichFaces with the context
parameter org.richfaces.resourceOptimization.enabled
set to true
, then the to-be-combined
resource cannot be resolved by a classpath URL due to RichFaces design limitations, so this combined resource handler
will use an internal workaround to get it to work anyway, but this involves firing a HTTP request for every resource.
The impact should however be relatively negligible as this is performed on localhost.
Modifier and Type | Field and Description |
---|---|
static String |
EXCLUDED_RESOURCES_PARAM_NAME
The context parameter name to specify resource identifiers which needs to be excluded from combining.
|
static String |
LIBRARY_NAME
The default library name of a combined resource.
|
static String |
SUPPRESSED_RESOURCES_PARAM_NAME
The context parameter name to specify resource identifiers which needs to be suppressed and removed.
|
LOCALE_PREFIX, RESOURCE_EXCLUDES_DEFAULT_VALUE, RESOURCE_EXCLUDES_PARAM_NAME, RESOURCE_IDENTIFIER
Constructor and Description |
---|
CombinedResourceHandler(ResourceHandler wrapped)
Creates a new instance of this combined resource handler which wraps the given resource handler.
|
Modifier and Type | Method and Description |
---|---|
Resource |
createResource(String resourceName,
String libraryName) |
ResourceHandler |
getWrapped() |
void |
handleResourceRequest(FacesContext context) |
boolean |
isListenerForSource(Object source)
Returns true if the source is an instance of
UIViewRoot . |
void |
processEvent(SystemEvent event)
Before rendering of a freshly created view, perform the following actions:
Collect all component resources from the head.
|
createResource, createResource, getRendererTypeForResourceName, isResourceRequest, libraryExists
public static final String LIBRARY_NAME
public static final String EXCLUDED_RESOURCES_PARAM_NAME
public static final String SUPPRESSED_RESOURCES_PARAM_NAME
public CombinedResourceHandler(ResourceHandler wrapped)
wrapped
- The resource handler to be wrapped.public boolean isListenerForSource(Object source)
UIViewRoot
.isListenerForSource
in interface SystemEventListener
public void processEvent(SystemEvent event) throws AbortProcessingException
processEvent
in interface SystemEventListener
AbortProcessingException
public Resource createResource(String resourceName, String libraryName)
createResource
in class ResourceHandlerWrapper
public void handleResourceRequest(FacesContext context) throws IOException
handleResourceRequest
in class ResourceHandlerWrapper
IOException
public ResourceHandler getWrapped()
getWrapped
in interface FacesWrapper<ResourceHandler>
getWrapped
in class ResourceHandlerWrapper