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.