0

I am trying to create a simple jersey-freemarker webapp, which I'm running with the jetty-maven-plugin. But the FreemarkerViewProcessor won't find my templates.

I have created a jersey resource at @Path("/") with a @GET public Viewable getIndex() method, that gets invoked once I request http://localhost:8080/. This Viewable is set to use the "index" template.

Assume the resource is com.example.MainResource (located in src/main/java/com/example/MainResource.java), and the index.ftl template file is located in src/main/webapp/templates/com/example/MainResource/index.ftl.

In my servlet setup, I tell jersey-freemarker about the templates directory, using a configuration Map: config.put(FreemarkerViewProcessor.FREEMARKER_TEMPLATES_BASE_PATH, "templates");. When debugging the FreemarkerViewProcessor, I see that it is indeed looking for the template resource based on the path /templates/com/example/MainResource/index.ftl.

Where should I put my template files so the FreemarkerViewProcessor will find them? Or how do I include my templates directory in the classpath?

PS: mvn jetty:run produces the following output (in part):

[INFO] Configuring Jetty for project: jettytest-servlet
[INFO] webAppSourceDirectory not set. Trying src/main/webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = ~/jettytest-servlet/target/classes
[INFO] Context path = /
[INFO] Tmp directory = ~/jettytest-servlet/target/tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:~/jettytest-servlet/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = ~/jettytest-servlet/src/main/webapp

Update:
The FreemarkerViewProcessor always uses a default Configuration, which sais to use a class-loader based template loader. As per @ddekany's comment, I should therefore place my templates/ in WEB-INF/classes/. To me that feels dirty; it seems like this should be taken care of by Maven.

Because of the FreemarkerViewProcessor's design (always using a class-loader configuration), I have posted this follow-up question. In the meantime, suggestions telling me how I can add the src/main/webapp/ directory to the classpath are welcome as well.

Community
  • 1
  • 1
derabbink
  • 2,419
  • 1
  • 22
  • 47
  • FreeMarker works with a virtual filesystem implemented by a `TemplateLoader` object. So you should check what kind of `TemplateLoader` is used there. Like if it's class-loader based, then certainly you should put the templates under `WEB-INF/classes/templates/...`. Another often used location is `WEB-INF/templates/...`. – ddekany Mar 19 '14 at 22:09
  • I have updated the question, and it seems like the `jersey-freemaker` implementation has some limitations. But I don't know how to overcome those yet. – derabbink Mar 20 '14 at 17:44

1 Answers1

0

As I found out here, using my own FreemarkerViewProcessor is not that difficult. So I copied the jersey-freemarker implementation and adapted it to use a Guice-injected Configuration object that I bind in my ServletModule.

Now I don't need the jersey-freemarker module any more, and I can skip putting stuff in the servlet config Map for freemarker.

Community
  • 1
  • 1
derabbink
  • 2,419
  • 1
  • 22
  • 47