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