4

What I am trying to accomplish is for some image references in a css file to be located in a folder seperate to the actual application.

Is it possible to mount an external folder as a resource in Wicket?

In pseudocode this is what I am trying to do:

public class Application extends WicketApplication
{
    init()
    {
        mountResource(new FolderResource("Path/to/some/folder", "someid"));
    }
}

So that the .css class would reference the resources like this:

.someclass
{
    url("resources/someid/images/image.png")
}

I'm sure I've seen this somewhere but I just can't seem to be able to find it again...

EDIT Should also note that im currently running on Wicket 1.4

David
  • 375
  • 4
  • 11
  • I think this StackOverflow question list all the possibilities that you have: http://stackoverflow.com/questions/5279232/images-referencing-in-css-with-wicket-for-hundreds-of-images. Personally I prefer and would try to implement the answer of martin-g. – Artem Shafranov Oct 02 '12 at 09:28

1 Answers1

3

As simple as following.

MyApplication.java:

public class MyApplication extends WebApplication {
    ...
    public void init() {
        ...
        final String resourceId = "images";
        getSharedResources().add(resourceId, new FolderResource(new File(getServletContext().getRealPath("img"))));    
        mountSharedResource("/image", Application.class.getName() + "/" + resourceId);
    }        
    ...
}

FolderResource.java:

public class FolderResource extends WebResource {

    private File folder;

    public FolderResource(File folder) {
        this.folder = folder;
    }

    @Override
    public IResourceStream getResourceStream() {
        String fileName = getParameters().getString("file");
        File file = new File(folder, fileName);
        return new FileResourceStream(file);
    }
}

And then you can get any image from "img" folder inside your application by simple URL:

/your-application/app/image?file=any-image.png

Here "/your-application" is the application context path, "/app" is the Wicket servlet mapping in web.xml, and "any-image.png" is the name of the image file.

Artem Shafranov
  • 2,655
  • 19
  • 19