【问题标题】:How can I add an image to Restlet template?如何将图像添加到 Restlet 模板?
【发布时间】:2017-01-07 20:56:57
【问题描述】:

我创建了一个资源:/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 文件的文件夹中

如何在模板页面上显示图像?

【问题讨论】:

    标签: java restlet restlet-2.0


    【解决方案1】:
    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,现在可以通过url:http://127.0.0.1:8888/test.jpg访问。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 2022-09-27
      • 1970-01-01
      • 2018-04-27
      • 2021-04-09
      相关资源
      最近更新 更多