Jersey 泽西自由标记
我正在开发一个基于jersey和freemarker的小工具,它将使设计师能够使用一些mok对象在本地测试freemarker模板 很抱歉写在这里,但除了一些代码和Javadoc之外,我找不到任何关于它的文档 为此,我做了以下工作: 1.依赖关系: 除了不渲染freemarker文件外,所有操作都正常。我有一个空白的白色页面,但文件存在,调试器在renderFtl方法中正确输入 你知道我怎么做吗 我在这里和网上读了很多文章,但是只有一些老文章或是关于spring集成的文章,我不想集成它,因为我不需要它 我真的很喜欢Jersey,我认为它是java世界上最完整、最强大的框架之一,但每当我试图查找有关特定功能或contribs库的文档时,我都会不知所措。。。无法逃离群组论坛:) 我在哪里可以找到关于它的完整文档 大卫,我有很多钱 更新: 试图解决这个问题时,我明白我不能使用内置的jersey支持,因为它需要使用放置在资源树中的文件。所以我所做的就是构建freemarker配置,在目前的测试中,直接@runtime并返回一个StreamingOutput对象:Jersey 泽西自由标记,jersey,freemarker,Jersey,Freemarker,我正在开发一个基于jersey和freemarker的小工具,它将使设计师能够使用一些mok对象在本地测试freemarker模板 很抱歉写在这里,但除了一些代码和Javadoc之外,我找不到任何关于它的文档 为此,我做了以下工作: 1.依赖关系: 除了不渲染freemarker文件外,所有操作都正常。我有一个空白的白色页面,但文件存在,调试器在renderFtl方法中正确输入 你知道我怎么做吗 我在这里和网上读了很多文章,但是只有一些老文章或是关于spring集成的文章,我不想集成它,因为我不
@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);
}
我并没有错误,调试和freemarker的证据告诉我,模板已找到并呈现,但泽西仍然并没有给我一个结果
我真的不知道为什么
[LastMatchedResourceClass].getResources(…)
可以找到模板,因此如果模板不能作为资源访问,则无法正确呈现。您可以签出Jersey源代码并将一些断点放入FreemarkerViewProcessor
,它应该会告诉您问题的确切位置您好,谢谢您的回复:)我正在使用1.9版的球衣,因为有一个错误,我更正了它。您好,谢谢您的回复!我使用的是1.9版本的jersey,因为有一个错误,我会更正它;)我下载了一些示例,试图将我的ftl文件移动到资源的同一位置,并以绝对静态的方式定位它,但我得到了相同的结果。此外,我需要在资源之外定位文件,因为它是一个测试工具。根据freemarker的需要,我有一个基本put,但我还需要在资源树之外有模板。。是否可以使用“干净”的方式?或者我必须在提供文件之前手动渲染它?好吧,对于当前的实现来说,这可能很难做到,但请随意以某种方式改进它。我可以想象一个支持绝对路径文件的可视类,我想这正是您需要的。实际上,这可以在(已经提到的)FreemarkerViewProcessor中完成,您只需要添加另一种解析模板的方法(添加一些代码,尝试直接打开文件,而不仅仅是使用Class.getResource(…)。是的,但要做到这一点,您需要实现TemplateProcessor,已弃用。在我的上一个应用程序中,我使用了这种方法,它运行良好,但我必须集成spring来推动freemarker配置,我不想将其用于此项目!非常感谢Pavel,我认为这是一个很好的共享线程;)请参阅com.sun.jersey.spi.template.ViewProcessor(替换已弃用的TemplateProcessor)。#发现错误时,我忘记了此处的@GET注释`@GET@Path({Path:([^\\s]+(\\\.(?I)(ftl))$)})公共流输出renderFtl(@PathParam(“Path”)字符串路径)引发异常{…}`如果您的目标是对UI使用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;
}
@Context ResourceConfig resourceConfig;
@Path("{path: ([^\\s]+(\\.(?i)(ftl))$)}")
public Viewable renderFtl (@PathParam("path") String path) throws IOException {
Viewable view = new Viewable("/"+path);
return view;
}
@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();
}
}
}