JSF: Serving Resources From a Jar
May 9, 2006 4 Comments
Many rich client JSF components use external resources of their own like scrips, stylesheets, images and etc. Suppose you have created one, packed in a jar and make it ready to distribute. The problem is that the user of your library must extract all the stuff if you’ve not added some tricks to your component to load the resources from the jar. If the resource is a script maybe it can be rendered onto the client however this will be a huge mistake. What happens if there are lots of scripts or even images, style sheets and etc. Good news is that there are various solutions you can employ for this problem.
If you are lucky to use JSF, this is your possible answer. A restore view listener will intervene the lifecycle, if the request is for a resource, it creates a url connection and streams it onto the client. The browser usually caches the resource. After the resource is streamed phaselistener calls responseComplete to stop the JSF lifecycle. Actually phaselistener can output anything using this way excel, image etc…
Lucky MyFaces users are familiar with this approach. MyFaces uses an extension filter to attach the resources demanded from the components(tomahawk).
Unlucky IBM JSF users are familiar with this one. It is based on the same principle with a phaselistener actually. A special request
comes to the servlet and it streams the resource onto the client.
Created only for this job. This one is an open source library aiming to ease the resource loading problem. Everything seems good at first glance but weblets are “a little bit” complicated. It uses it’s own view handler and yet another metadata weblets-config.xml and more…
Filter and Servlet is not specific to JSF but can also be used. I think Phaselistener approach is the simplest and the most convenient way if you are using JSF. I’ve used and enjoyed it when working on facestrace and JSF client side validators. The main advantage is that both filter and servlet must be declared-mapped at web.xml, on the other hand since faces-config.xml’s at classpath are merged, there is no need to declare any metadata for a phaselistener. Weblets are also another JSF alternative, I’ve never used them because I love phaselisteners more than anything in my entire life. Actually real reason is that weblets seem to be too complex for the job when compared to a simple one class phaselistener.
More information on these techniques;
- Source code and the documentation of an example of a ResourcePhaselistener
- MyFaces ExtensionsFilter
- Weblets Project