如何将图像添加到Restlet模板?

时间:2017-01-07 20:56:58

标签: java restlet restlet-2.0

我创建了一个资源:/ accounts / {accountId},它使用类 AccountServerResource.class,AccountPage.class和模板accountPage.ftl

出于测试目的,我创建了一个非常简单的模板,只包含一个字符串:

<h1>Hello world</h1>

页面localhost:8111 / accounts / 21正确显示。

现在我想进一步向资源添加更多信息。我首先尝试做的是在模板中添加图像:

<h1>Hello, world</h1> <img src="img/user21.jpg"> 

但这次没有显示图像。我有一个错误:找不到资源localhost:8111 / accounts / 21 / img / user21.jpg。文件夹 img 存储在包含所有* .class文件和* .ftl文件的文件夹中

如何在模板页面上公开图像?

1 个答案:

答案 0 :(得分:0)

public class TestStaticFile {
    public static void main(String[] args) {
        Component component = new Component();
        Application application = new Application() {
            @Override
            public Restlet createInboundRoot() {
                Directory dir = new Directory(getContext(), "file:///d:/test");
                dir.setListingAllowed(true);
                return dir;
            }
        };
        component.getServers().add(new Server(Protocol.HTTP, 8888));
        component.getClients().add(Protocol.FILE);
        component.getDefaultHost().attach(application);
        try {
            component.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如果图片的路径为D:/test/test.jpg,现在您可以使用网址http://127.0.0.1:8888/test.jpg进行访问。

您可以参考要在img标签中显示的图像的链接。 Restlet支持静态文件。将图像保存在指定的文件夹中,然后在代码中引用它。

重定向用户指南中的Static files设置可能会有所帮助。