我正在开发一个基于jersey和freemarker的小工具,这将使设计人员能够使用一些mok-objects在本地测试freemarker模板。
我很抱歉在这里写,但我找不到任何关于它的文档,除了一些代码和javadoc。
为此,我做了以下事情:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-freemarker</artifactId>
<version>1.9</version>
</dependency>
protected static HttpServer startServer() throws IOException {
System.out.println("Starting grizzly...");
Map<String, Object> params = new HashMap<String, Object>();
params.put("com.sun.jersey.freemarker.templateBasePath", "/");
ResourceConfig rc = new PackagesResourceConfig("resource.package");
rc.setPropertiesAndFeatures(params);
HttpServer server = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
server.getServerConfiguration().addHttpHandler(
new StaticHttpHandler("/libs"), "/libs");
return server;
}
@Context ResourceConfig resourceConfig;
@Path("{path: ([^\\s]+(\\.(?i)(ftl))$)}")
public Viewable renderFtl (@PathParam("path") String path) throws IOException {
Viewable view = new Viewable("/"+path);
return view;
}
一切正常,但不会呈现freemarker文件。我有一个空的白页,但文件存在,调试器右边的renderFtl方法输入。
你知道我该怎么办?
我在这里和网络上阅读了很多文章,但只有旧帖子或文章谈论Spring集成,我不想整合它,因为我不需要它。
我真的很喜欢泽西岛,我认为它是java世界上最完整和最强大的框架之一,但是每当我试图找到特定功能或贡献库的文档时,我都迷失了......没有逃离群组论坛:)
我在哪里可以找到有关它的完整文档?
坦克很多大卫
试图解决我明白我不能使用内置的球衣支持,因为它需要使用放在资源树中的文件。所以我所做的就是直接在@runtime中测试freemarker配置并返回一个StreamingOutput对象:
@Path("{path: ([^\\s]+(\\.(?i)(ftl))$)}")
public StreamingOutput renderFtl (@PathParam("path") String path) throws Exception {
Configuration cfg = new Configuration();
// Specify the data source where the template files come from.
// Here I set a file directory for it:
cfg.setDirectoryForTemplateLoading(new File("."));
// Create the root hash
Map<String, Object> root = new HashMap<String, Object>();
Template temp = cfg.getTemplate(path);
return new FTLOutput(root, temp);
}
这不是一个好的代码,但仅供测试...
class FTLOutput implements StreamingOutput {
private Object root;
private Template t;
public FTLOutput(Object root, Template t) {
this.root = root;
this.t = t;
}
@Override
public void write(OutputStream output) throws IOException {
Writer writer = new OutputStreamWriter(output);
try {
t.process(root, writer);
writer.flush();
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我没有关于调试的错误证据,freemarker告诉我模板被找到并呈现,但是jersey仍然没有给我一个结果......
我真的不知道为什么!
答案 0 :(得分:1)
你为什么使用Jersey 1.9? 1.11已经出来,如果可以,你应该更新
你见过"freemarker" sample from Jersey吗?它展示了使用带有球衣的freemarker的简单用法。
您的资源在哪里?
通过调用[LastMatchedResourceClass].getResources(...)
找到模板,因此如果您的模板无法作为资源访问,则无法正确呈现它们。你可以查看泽西岛的来源并将一些断点放入FreemarkerViewProcessor
,它应该告诉你究竟问题在哪里..