CSS referenced images using Wicket for hundreds of images

As in other nice posts, we can use something like this for a single image:

mountSharedResource("/images/logo.gif", new ResourceReference(ImageScope.class, "logo.gif").getSharedResourceKey()); 

But what if we have 100 images needed for sharing? To map them 1 on 1, this is not the case at all. Is there any good way to automatically install all the images from 1 directory?

If not, I'm going to write some support that makes mountSharedResource in a loop automatically for all the images that are in the directory of a particular image. But in fact, I'm not sure if this is correct.

Thanks.

+4
source share
3 answers

Well, I solved the problem as follows:

 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 is unbelievable that Wicket does not support him in any way internally. I'm very surprised. To install pages with a bookmark for the entire package, we mount (path, package name), but nothing for resources (if I understand correctly).

+2
source

This will kill the performance of your application. It’s better to mount one shared resource for all images, and then, depending on the request parameters transferred, load different images and replenish them back in response.

0
source

I'm not sure if this is what you need, but I found the following snippet to help me with a similar problem:

 getMarkupSettings().setAutomaticLinking(true); 

I found out about it on the Wicket Wiki .

0
source

Source: https://habr.com/ru/post/1343388/


All Articles