Set the external folder as a resource in Wicket

I am trying to make some links to images in a css file that will be located in a folder separate for the real application.

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

In pseudo code, this is what I am trying to do:

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

For the .css class to reference resources such as this:

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

I'm sure I saw it somewhere, but I just can't find it again ...

EDIT It should also be noted that im is currently running on Wicket 1.4

+4
source share
1 answer

Easier as shown below.

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 the "img" folder inside your application at a simple URL:

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

Here "/ your-application" is the application context path, "/ app" is the conversion of Wicket servlets to web.xml, and "any-image.png" is the name of the image file.

+3
source

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


All Articles