Search code examples
jsfstaticjaricefacescustom-component

How to get an image or js from a JAR in the server in JSF (For a custom component)


I have done a custom JSF component (it looks like a combobox (icefaces selectonemenu)) but it uses a couple of images (png) and a bit of javascript. I jar everything, so then the developers use it as a jar copied in the web-inf/lib folder. The image and the js are just for this custom component, so I can't make them put this image and js in his project, it has to be in MY jar.

I jar everything and it works almost great, just the image and the JS, I do not get them to work. I do not know how to reference them being in the jar. I could make them work as long as they are part of the application, but not being part of the jar. How should I do to get them in my encodebegin code for example?

I am using JSF with icefaces 1.8

Thanks in advance!!


Solution

  • If you're already on JSF 2.0, it should work just fine out the box when you're using @ResourceDependency or @ResourceDependencies annotation which can resolve resources based on JAR's /META-INF/resources folder.

    On JSF 1.x, your best bet is to create a custom "resource servlet" which is mapped on a certain URL pattern, e.g. /com.nahiko.resources/* and just streams the resources from the classpath to the HTTP response. Basic kickoff example:

    String path = "/META-INF/resources" + request.getPathInfo();
    InputStream input = getClass().getResourceAsStream(path);
    OutputStream output = response.getOutputStream();
    // ...
    

    Document along your JAR that this servlet has to be mapped. Or if you target a Servlet 3.0 container, just add the @WebServlet annotation to get it to auto-register.