简体   繁体   English

在Wicket中将外部文件夹作为资源挂载

[英]Mount an external folder as a resource in Wicket

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. 我想要完成的是将css文件中的某些图像引用放在与实际应用程序分开的文件夹中。

Is it possible to mount an external folder as a resource in Wicket? 是否可以将外部文件夹作为资源安装在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: 这样.css类就会像这样引用资源:

.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 编辑还应该注意到我目前在Wicket 1.4上运行

As simple as following. 简单如下。

MyApplication.java: 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: 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: 然后,您可以通过简单的URL从应用程序内的“img”文件夹中获取任何图像:

/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. 这里“/ your-application”是应用程序上下文路径,“/ app”是web.xml中的Wicket servlet映射,“any-image.png”是映像文件的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM