users@jersey.java.net

Re: [Jersey] Problems with Paths and FreeMarker TemplateProcessor

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 10 Jun 2009 18:37:46 +0200

Hi Jonathan,

See here for another Freemarker template processor that works with Web
apps:

   http://www.cwinters.com/blog/2009/04/03/jersey_freemarker_provider.html

Perhaps Chris knows a thing or two about Freemarker to help you out.


Are you using Servlet or another type of HTTP container?

This seems like some sort of Freemarker config issue to the location
of where your templates are. Perhaps you could test your assumptions
outside of Jersey by writing a little test app that attempts to get
the template directly ?

If you can wrap up a simple Jersey test app from within a maven
project i might be able to help you out with some experimentation.

Paul.



On Jun 10, 2009, at 5:55 PM, Jonathan Cook - FM&T wrote:

> Hello,
>
> I would very much appreciate some help.
>
> I am trying to get a simple FreeMarkerTemplateProcessor setup and have
> read the following useful blog http://blogs.sun.com/sandoz/entry/mvcj
> and a couple of other posts.
>
> But I am having some problems actually resolving the template. I
> have a
> directory in my project called Resouces/freemarkertemplates and I have
> put a very simple test.ftl template in that directory:
>
> <html>
> <h2>Hello World</h2>
> </html>
>
> This is my FreemarkerTemplateProcessor: (its a bit hacked as just want
> to get something working)
>
> @Provider
> @Singleton
> public class FreemarkerTemplateProcessor implements
> TemplateProcessor {
> private static final Logger log =
> Logger.getLogger(FreemarkerTemplateProcessor.class);
>
> private Configuration config;
>
> private synchronized void init() {
> try{
> config = new Configuration();
> config.setClassForTemplateLoading(this.getClass(),
> "Resources/freemarkertemplates" );
> log.debug("loaded");
> } catch(Exception e){
> log.error("Error loading template directory", e);
> }
> }
>
> public String resolve(String path) {
> if (this.config == null) {
> init();
> }
>
> final String filePath = path.endsWith( "ftl" ) ? path : path +
> ".ftl";
> log.debug("Path= "+filePath);
>
> try {
> if (config.getTemplate(path) == null){
> return null;
> }
> } catch (IOException e) {
> e.printStackTrace();
> return null;
> }
> return filePath;
> }
>
> public void writeTo(String resolvedPath, Object object, OutputStream
> out)
> throws IOException {
> /*Map<String, Object> model = new HashMap<String, Object>();
> model.put("resource", object); */
> try {
> log.debug(resolvedPath);
> this.config.getTemplate(resolvedPath).process(object, new
> OutputStreamWriter(out));
> } catch (TemplateException e) {
> throw new WebApplicationException(e);
> }
> }
> }
>
> Then a simple resource:
> @Path("includepathgenerator")
> public class FootballIncludePathGeneratorResource {
>
> private static final Logger log =
> Logger.getLogger(FootballIncludePathGeneratorResource.class);
>
> public String id = "Hello";
>
> @GET
> public Viewable getBlank() {
> return new Viewable( "/test.ftl", this );
> }
> }
>
> But I get an error when running this and going to the page:
> 2009-06-10 16:40:25,540 DEBUG [btpool0-1]
> resources.FreemarkerTemplateProcessor - Path= /test.ftl
> 2009-06-10 16:40:25,540 DEBUG [btpool0-1] freemarker.cache - Could not
> find template in cache, creating new one;
> id=[test.ftl[en_GB,Cp1252,parsed] ]
> java.io.FileNotFoundException: Template /test.ftl not found.
> at
> freemarker.template.Configuration.getTemplate(Configuration.java:489)
>
> I have tried various alternatives like passing in different paths to
> the
> Viewable.
> /Resources/freemarkertemplates/test.ftl and also tried just passing in
> test.ftl and putting the the test.ftl in the same directory as the
> resource but that doesn't work. I also tried using
> setDirectoryForTemplateLoading but that didn't work either. Maybe it
> is
> something along this line though?
>
> The Resources/freemarkertemplates dir is on the classpath:
> <classpathentry kind="lib" path="Resources/freemarkertemplates"/>
>
> There must be a way to sort this out I'm hoping?
>
> Thanks
> Jonathan
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>