JSF: Serving Resources From a Jar

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.

1) Phaselistener
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…

2) Filter
Lucky MyFaces users are familiar with this approach. MyFaces uses an extension filter to attach the resources demanded from the components(tomahawk).

3) Servlet
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.

4) Weblets
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…

Comparison
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;

4 Responses to JSF: Serving Resources From a Jar

  1. Jacob Hookom says:

    I have another working solution such that anything located in /FACES-INF within your jars is automatically public. So it’s much easier to setup/configure public resource, knowing they will always be available without ‘specialized’ requests.

  2. Cagatay says:

    Hi Jacob, good one, I have a feeling that your solution is specific to Facelets and there is a custom view handler, is my guess right?

  3. Jacob Hookom says:

    It was done with a Filter, since it’s actually not specific to the JSF API, you just say that if the requested url starts with /FACES-INF/*, then use the classloader to do: classloader.getResource(uri.substring(1)); I totally realize that you don’t want to expose your classloader, but this precheck and contract with the developer is simple to understand.

  4. Sumon says:

    Hi,

    Is there any way that we can outputstream a xhtml(facelets) page from a *.jar!/META-INF?

    Basically I would like to build a jar with some js, css, img, faces-config.xml java classes and xhtml(facelets). It seems to work other than *.xhtml. I tried with PhaseListener and filter to load those *.xhtml but they doesn’t render to html and returns the row xhtml file.

    I appreciate your reply.

    Regards,
    Sumon

%d bloggers like this: