Freemarker template not found

I'm currently trying to get Freemarker to work with my application using Spring. No matter what I try, I keep getting a pattern that is not found. I'm not sure if the setting is configured correctly, but never finds my template. Here is my spring bean config:

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/> </bean> 

Whenever I try to call getTemplate in freemaker configuration, it always sends back a template that is not found. So if i do

 configuration.getTemplate("testTemplate.ftl") 

it always throws an IOException.

I'm not sure if anyone has any idea what I'm doing wrong.

Thanks for your help!

+4
source share
3 answers

First of all, /WEB-INF/freemarker will only work as a path from WebApplicationContext ; otherwise, Spring will try to resolve it as a path to the file system, not a servlet context path. Have you subtracted the excerpt from the context loaded by the DispatcherServlet ?

Secondly, is there a reason you use configuration directly instead of using Spring ViewResolver ?

Finally, an IOException can mean many different things. Can you post a full stack trace?

0
source

I think you should make sure that the file "testTemplate.ftl" is in the folder "/ WEB-INF / freemarker /"

0
source

I had the same problem and in the end I decided to use the following approach:

 Configuration configuration = new Configuration(); FileTemplateLoader templateLoader = new FileTemplateLoader(new File(YOUR_BASE_TEMPLATE_DIR)); configuration.setTemplateLoader(templateLoader); freemarker.template.Template template = configuration.getTemplate(YOUR_TEMPLATE_NAME); template.process(datamodel, writer); 
-one
source

All Articles