2017-01-07 18 views
0

我創建了一個資源:/ accounts/{accountId},它使用類AccountServerResource.class,AccountPage.class和模板accountPage.ftl如何將圖像添加到Restlet模板?

只是用於測試目的,我創建了一個包含只是一個串一個非常簡單的模板:

<h1>Hello world</h1>

頁本地主機:/ 21正確顯示8111 /帳戶。

現在我想更進一步並向資源添加更多信息。我第一次嘗試做的,是添加圖像到模板:

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

但此時不顯示圖像。我有一個錯誤:找不到資源localhost:8111/accounts/21/img/user21.jpg。文件夾img存儲在包含所有* .class文件和* .ftl文件的文件夾中

如何在模板頁上顯示圖像?

回答

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,現在你可以通過URL訪問:http://127.0.0.1:8888/test.jpg

您可以參考您想要在img標籤中顯示的圖像的鏈接。 Restlet支持靜態文件。將圖像保存在指定的文件夾中,然後在您的代碼中引用它。

restlet用戶指南中的Static files設置可能有所幫助。