1

I really don't know if it's a Netbeans issue but here's the problem: In my jsf web app, in the root directory, I have a folder named resources which contains the sub folders images, css and js (for javascript). The subfolders contain the respective content. Here's how I reference a script (for example) from within the application:

   <h:outputScript library="js" target="head" name="dialog.js"/>

Problem is, when I clean and build, while the files in the css directories and js directories are left intact, all the images in the images directory are deleted. How then can I use the jsf resource mechanism if it'll wipe off my images on every "clean and build"?

Emmanuel
  • 322
  • 2
  • 12

1 Answers1

1

JSF doesn't do that. It's the server which does that. This problem indicates that you're manually storing (uploaded?) images in the expanded WAR folder instead of somewhere outside it. A "clean and rebuild" just cleans the server's work folder and the old expanded WARs and rebuilds the webapp based on the original WAR (essentially, when developing, the project's structure). This does by the way not only happen in development, but also in production whenever you redeploy a new WAR file.

This behaviour is by specification. You should simply not manually put files in the expanded WAR folder and expect them to magically be included in the original WAR file or to be retained on redeploy.

Store them in a fixed and permanent location outside the WAR. If you add the external path as a new context to the server, then you can just reference them by <img> or <h:graphicImage value> the usual way. See also Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag.

If you really need to get them to be resolved by the JSF resource handler, so that you can use them in something like <h:graphicImage library="uploads" name="foo.png>, then you need to implement a custom ResourceHandler. It look something like this:

public class UploadedResourceHandler extends ResourceHandlerWrapper {

    private ResourceHandler wrapped;

    public MyResourceHandler(ResourceHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ResourceHandler getWrapped() {
        return this.wrapped;
    }

    @Override
    public Resource createResource(String resourceName, String libraryName) {
        if ("uploads".equals(libraryName)) {
            return new YourCustomUploadedResourceImpl(resourceName);
        } else {
            return super.createResource(resourceName, libraryName);
        }
    }

}

To get it to run, register it as follows in faces-config.xml:

<application>
    <resource-handler>com.example.UploadedResourceHandler</resource-handler>
</application>

This is however not exactly trivial to summarize off top of head in a single answer. You also need to understand how HTTP (caching) works. Just read the javadocs, starting at ResourceHandler, and peek around in Mojarra's own ResourceHandlerImpl and ResourceImpl source codes to get the idea.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • My my! BalusC, you're a legend! Really, how did you get so good? – Emmanuel Aug 29 '12 at 12:14
  • Perhaps. But, pardon me, I have to insist there's more to it. You're 'only' 34 (your profile says), and so with the in-depth knowledge you have of these technologies - some that imply you read technology specifications while most of us use textbooks - it's obvious there's more than 'years' to it. 61,099 profile views says I'm not the only one who thinks so. You're the kind of person I wish I had at my place of work. I'd 'suck' all I could out of you :-) Wish I knew the secret. Really wish. Maybe you should write some stuff to help lil' folks like me try to get badass. – Emmanuel Aug 29 '12 at 16:21
  • 1
    Well, it's maybe the combination of a somewhat photographic memory, the very strong logical-thinking skills (as a 10 year old kid I was tested with IQ of 160 on that area), the eagerness to figure out and really understand how technical stuff works "under the covers", the ability to explain even the most complex things in simplest terms to others so that they really get enlightenend. – BalusC Aug 29 '12 at 16:26