泽西自由标记
我正在开发一个基于 jersey 和 freemarker 的小工具,这将使设计人员能够使用一些 mok 对象在本地测试 freemarker 模板。
很抱歉在这里写下,但除了一些代码和 javadoc 之外,我找不到任何有关它的文档。
为此,我执行了以下操作:
1 依赖项:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-freemarker</artifactId>
<version>1.9</version>
</dependency>
2 启动 grizzly,告诉在哪里找到 freemarker 模板:
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;
}
3 创建根资源并绑定 freemarker 文件:
@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 集成的文章,我不想集成它,因为我不需要它。
逃离团体论坛:)
我真的很喜欢 Jersey,我认为它是 Java 世界上最完整、最强大的框架之一,但每当我尝试查找有关特定功能或贡献库的文档时,我都会迷失……没有办法 我可以找到有关它的完整文档吗?
坦克很多大卫
更新:
试图解决我明白我不能使用内置球衣支持,因为它需要使用放置在资源树中的文件。所以我所做的是构建 freemarker 配置,现在在测试中,直接 @runtime 并返回一个 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);
}
FTLOutput 在这里:
这不是一个好的代码,但仅用于测试......
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 告诉我模板已找到并呈现,但球衣仍然没有给我结果......
我真的不知道为什么!
I'm developing a small tool based on jersey and freemarker, which will enable designers to test there freemarker templates, locally, using some mok-objects.
I'm sorry to write here, but I cant find any documentation about it except some code and javadocs.
To do that I did the following:
1 Dependencies:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-freemarker</artifactId>
<version>1.9</version>
</dependency>
2 Starting grizzly, telling where to find freemarker templates:
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;
}
3 Creates the root resource and binds freemarker files:
@Context ResourceConfig resourceConfig;
@Path("{path: ([^\\s]+(\\.(?i)(ftl))$)}")
public Viewable renderFtl (@PathParam("path") String path) throws IOException {
Viewable view = new Viewable("/"+path);
return view;
}
Everything works fine, except that freemarker files are not rendered. I have an empty white page, but file exists and debugger enter inside renderFtl method right.
Do you know how can I do that?
I read a lot of articles here and around the web, but old posts only or articles talking about spring integration and I don't want to integrate it because I don't need it.
I really like Jersey, I think is one of the most complete and power framework on java world, but anytime I try to find documentation on specific features or contribs libraries, I'm lost... There no escape from groups forums :)
Where can I find a complete documentation about it?
Tanks a lot David
Updates:
Trying to solve I understood I cannot use built-in jersey support, because it needs to use files placed in resources tree. So What I did is to build freemarker configuration, in test for now, directly @runtime and returns a StreamingOutput object:
@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);
}
FTLOutput is here:
This is not a good code, but is for test only...
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();
}
}
}
I have no errors evidence on debug and freemarker tells me that template is found and rendered, but jersey still no give me a result...
I really don't know why!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么使用 Jersey 1.9? 1.11 已经发布了,如果可以的话你应该更新
你见过 来自泽西岛的“freemarker”样本?它演示了将 freemarker 与球衣一起使用的简单用例。
你的资源在哪里?
模板是通过调用
[LastMatchedResourceClass].getResources(...)
来查找的,因此如果您的模板无法作为资源访问,则无法正确呈现它们。你可以查看 Jersey 源代码并在 FreemarkerViewProcessor 中放置一些断点,它应该告诉你问题到底出在哪里..Why are you using Jersey 1.9? 1.11 is already out, you should update if you can
Have you seen "freemarker" sample from Jersey? It demonstrates simple usecase of using freemarker with jersey.
Where are your resources?
Templates are being found by calling
[LastMatchedResourceClass].getResources(...)
, so if your templates are not accessible as resources, they can't be rendered correctly. you can checkout Jersey source and place some breakpoints intoFreemarkerViewProcessor
, it should tell you where exactly the problem is..