Hi Paul,
Thanks for the comments. I'd seen Chris's blog entry as well. I
eventually got it working after stepping through the Freemarker source.
It was a
little bit weird as the path in my unit test was working but when I put
the same path into my FreemarkerTemplateProcessor it didn't work but
after
changing this path everything was ok so a bit of a red herring in the
end.
One other quick question, you might remember, we're using Jetty with
Jersey so I have this line of code:
servletHolder.setInitParameter("com.sun.ws.rest.config.property.packages
",
"com.bbc.newsi.feeds.sport.webservice.football.resources");
But I didn't want to put my FreemarkerTemplateProcessor in that
resources package, it would live outside of football. Is it possible to
register
the providers some other way or specify multiple packages. I didn't
really want to use the the * notation as we have other resources not
specific to
football that I don't want to register with this instance if that makes
sense.
Maybe when I've finished what I'm working on it might be useful to add
this as an example? I'd be quite keen to contribute and have recently
been
contributing to apache camel as well. You might be intersted to know as
well I did some work which generates graphical sports statistics
dynamically
on the fly using Jersey as well. Similar idea to google charts.
Thanks
Jon
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
>
Jonathan Cook
Software Engineer
Feeds Team
BBC Future Media & Technology (Journalism)
BC3 B1, Broadcast Centre
T: 020 800 84734 (02 84734)