[英]How can I add an image to Restlet template?
我创建了一个资源:/ 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文件的文件夹中
如何在模板页面上显示图像?
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
: http://127.0.0.1:8888/test.jpg
D:/test/test.jpg
。
您可以在img标签中引用要显示的图像的链接。 Restlet支持静态文件。 将图像保存在指定的文件夹中,然后在代码中引用它。
restlet用户指南中的静态文件设置可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.