Using RichFaces to give versioned URL's

RichFaces has some nice components that can load scripts and styles when placed anywhere in the xhtml page and automatically inserts them into the header at render time. For example a footer could say it requires stylesheet, after you've already defined a header.

The tags are pretty simple there is the style loader, loadStyle:
<afj:loadstyle src="/css/test.css"></afj:loadstyle>
And the script loader, loadScript:
<afj:loadscript src="/javascript/test.js">

The src can be a path be:
  1. Relative to the current page.
  2. Path relative to the context if starting with a slash.
  3. Or loaded by RichFaces resource framework
    Using "resource:///path/to/file"
Now, the problem most web developers always face is that of caching. If you disable caching then the site is slow for the user and your bandwidth is used up. If you enable caching and release a new version, how do you clear the user's and ISP's cache so they can see your changes?

RichFaces already deals with this by creating virtual URL's for all their resources served (the static javascript files and the generated css files and images). It prepends "afj/g/[version]" to the url's. For example:
<script src="/myapp/jsf/a4j/g/3_3_2.SR1jquery.js" type="text/javascript">
Where myapp is the name of the context and I have a mapping to the faces servlet for /jsf/*. This gets prepended to all resources served through the framework, even ones from your war.

This neat little feature can be used to version your own deployments, so that after each build a new URL is generated.

If your using maven its simple.
First add this to your web.xml:
  not used, but needs to be set
  the prefix used by richfaces for global cached resources
  the prefix used by richfaces for session cached resources
Next you'll need to add the buildnumber plugin:
If your using eclipse to do development then you'll probably want to have 2 web.xml files. One that eclipse can read with the filtering (property subsititution turned off) and one for production builds.


  1. Check your spelling:

    If your using maven its simple.
    First add this to your web.xml:

    This should be:
    If you're using maven, it's simple.
    First add this to your web.xml:



Post a Comment