Jersey-FreeMarker

I am developing a small tool based on knitwear and freemarker, which will allow designers to test freemarker templates locally using some mok objects.

Sorry to write here, but I can not find documentation about this except code and javadocs.

To do this, I did the following:

1 Dependencies:

<dependency> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-freemarker</artifactId> <version>1.9</version> </dependency> 

2 Running 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 a root resource and links 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 displayed. I have a blank white page, but the file exists, and the debugger correctly enters the renderFtl method.

Do you know how I can do this?

I read many articles here and around the Internet, but only old posts 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 that this is one of the most complete and powerful frameworks in the java world, but at any time I try to find documentation on specific functions or library contributors, I am lost ... There is no way out of forum groups :)

Where can I find complete documentation about this?

Tanks a lot david

Update:

Trying to solve I realized that I canโ€™t use the built-in jersey support, because it needs to use files located in the resource tree. So, I did to build the freemarker configuration, at the moment, 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 here:

This is not good code, but only for the test ...

 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 evidence of debugging errors, and freemarker tells me that the template was found and displayed, but Jersey still does not give me the result ...

I really don't know why!

+4
source share
1 answer
  • Why are you using Jersey 1.9? 1.11 is already out, you should upgrade if you can

  • Have you seen the "freemarker" sample from Jersey ? This demonstrates an easy way to use freemarker with a shirt.

  • Where are your resources? Patterns are found by calling [LastMatchedResourceClass].getResources(...) , so if your patterns are not available as resources, they cannot be displayed correctly. you can check the source of the jersey and put some breakpoints in the FreemarkerViewProcessor , it should tell you exactly where the problem is.

+1
source

Source: https://habr.com/ru/post/1397749/


All Articles