Images referencing in CSS with Wicket for hundreds of images
As from another nice posts, we can use something like this for 1 single image:
mountSharedResource("/images/logo.gif", new ResourceReference(ImageScope.class, "logo.gif").getSharedResourceKey());
But what if we have 100 images needed to be shared ? To map them 1 by 1 is the no way actually. Is there some nice way how to achieve mounting of all images of 1 directory automatically ?
If not, I am thinking about to write some support doing the mountSharedResource in a loop automatically for all images residing in the concrete image directory. But actually I a开发者_StackOverflowm not sure if it's the right direction.
Thanks.
Well, I solved the issue by this:
private void mountResources(Class clazz, String directory) {
java.net.URL url = clazz.getResource(clazz.getSimpleName() + ".class");
File[] files = new File(url.getPath()).getParentFile().listFiles();
for (int i=0; i< files.length; i++) {
String fileName = files[i].getName();
if (!fileName.endsWith("class")) {
mountSharedResource("/" + directory + "/" + fileName, new ResourceReference(clazz, fileName).getSharedResourceKey());
}
}
}
And using it like this:
mountResources(ImagesScope.class, "images");
mountResources(FontsScope.class, "fonts");
mountResources(JsScope.class, "js");
But it's unbelievable that Wicket doesn't support it somehow internally. I am quite surprised. For mounting bookmarkable pages for the whole package we have mount(path, packageName), but for resources nothing (if I understand it right).
This will kill the performance of your app. Better mount one shared resource for all images and then depending on the passed request parameters load different images and stream them back in the response.
I'm not sure this is what you need, but I found the following snippet to help me with a similar issue:
getMarkupSettings().setAutomaticLinking(true);
I found out about it on Wicket's Wiki.
精彩评论