2012-05-12 23 views
0

我在使用Restlet模板表示形式时遇到问题。获取restlet模板表示形式

我使用的模板表示,因为我有3页是从thecontent除了类似(即标题,导航和页脚是相同的): 的index.html,services.html,contact.html 使用模板表示将是有用的,因为如果我需要更改导航栏或页脚,那么我将只需要在一个地方完成(template.html)

我已将FreeMarker罐添加到我的构建路径,并且可以访问模板数据。

我想一些基本的东西,开始与:

@Get 
public Representation represent() { 
    return new TemplateRepresentation("template.html", new Configuration(), MediaType.TEXT_HTML); 
    } 

我希望无论是在template.html显示在浏览器上。但我得到控制台中的错误No template found

这是我的文件结构的减少版本。

Java Resources 
    - src 
     - Application.java 
     - IndexResource.java (This class contains the template representation to show the index page) 

Web Content 
    - WEB-INF 
    - template.html 
+0

尝试将日志级别调高为DEBUG或ALL,以便您可以看到TemplateRepresentation类正在尝试执行的操作。 template.html文件不是TemplateRepresentation期望找到的地方,而且日志很可能会告诉你它在哪里查找。 – jmort253

回答

1

事实上,我认为你需要在配置实例中指定一个适当的模板加载器。这将允许的Freemarker知道在哪里以及如何找到模板...

org.restlet.Context context = (...) 
Configuration configuration = new Configuration(); 

ContextTemplateLoader loader1 = new ContextTemplateLoader(context, "file://<DIR1>"); 
ContextTemplateLoader loader2 = new ContextTemplateLoader(context, "file://<DIR2>"); 

MultiTemplateLoader loaders = new MultiTemplateLoader(
       new TemplateLoader[] { loader1, loader2 }); 

configuration.setTemplateLoader(loaders); 

你可以在地址找到TemplateLoader接口的所有受支持的实现:http://freemarker.sourceforge.net/docs/api/freemarker/cache/TemplateLoader.html。我认为WebappTemplateLoader实现可能是你正在寻找的...

希望它可以帮助你。 Thierry