Note: The implementation contains issue, thus the samples from this blog needs to be slightly modified, details in this comment. The fix will be available with RichFaces 4.2.1.CR1.
RichFaces resource mapping can save your life when you need to serve a different resource (JS, CSS, image) file than the one originally requested. It works in the stage of determination of the resource request path.
Specifically in all following situations, it may be really handy:
- providing alternative versions of a resource
- map several resources to one
- using external resources
- moving resources to servers with static content
Before diving deeper into the situations above, let's look at how resources typically works in JSF.
|Component libraries (JARs) and application web archive (WAR)|
and resource dependencies (green)
Let's look at all of mentioned situations one by one:
Providing alternative file
For example, your application requests jquery.js resource, but you don’t want to use default one, you want to provide alternative, maybe patched version to solve some issues. So you provide RichFaces the mapping using following configuration.
Create the file META-INF/richfaces/static-resource-mappings.properties on the classpath of your project and configure one mapping:
All requests for jquery.js will then be served as requests for jquery-alternative-version.js.
|jquery.js mapped to jquery-alternate.js|
Warning: Resource mapping requires resource servlet for its work - it is automatically registered in Servlets 3.0 environments (JBoss AS 6 and 7, Tomcat 7, GlassFish 3, etc.), but you will need to register that manually in Servlets 2.5 or lower environments - see RichFaces Developer Guide for details of how configure it.
Note: This mapping file needs to be placed in one of following locations:
Note: jquery-alternative-version.js needs to be placed in your project on one of following locations (JSF resource:Another example
Or you can similarly map jsf.js resource:
Note: notice the backslash before the : (it is the escape sequence, required in the properties file)
Map several resources to one
Another requirement comes when you are using several component libraries in one project - oh crap, and they all are based on jQuery and each of them uses another version!
One of solutions here (except using jQuery.noConflict()) is map all requests for different jquery.js versions to one. Let’s define following mapping:
# RichFaces bundled jQuery (following line is not necessary)
# PrimeFaces bundled jQuery
# Another project bundled jQuery
Okay, now all these libraries use only one version of jQuery - the RichFaces one. ;-)
|another:jquery.js mapped to jquery.js|
Using external resources
But resource mapping isn't used only for mapping requests to serve local resources, but external HTTP resources can be served as well.
Let’s show-case this on sample of mapping requests of jQuery library to CDN .
|jquery.js mapped to CDN resource|
Moving resources to servers with static content
With RichFaces, you can even move all your resources to a server which serves static requests (like Apache Httpd) to lighten your application server. Just map all your resources to HTTP locations.
|All application resources are mapped to static server.|
That's it, RichFaces Resource Mapping really can save your life! ;-)