5

I have a Spring/JSF Web application which has a dependency to a module uses Freemarker templates. Here is what i did for integration:

I imported the applicationContext-freemarker-module.xml to applicationContext.xml I added the configuration bean to applicationContext-freemarker-module.xml like below.

 <bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
   <property name="templateLoaderPath" value="classpath*:/"/>
 </bean>

I put my templates to src/main/resources directory of freemarker module. I am reading the templates like below:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-freemarker-module.xml");

Configuration templateConfig = (Configuration) context.getBean("freemarkerConfiguration");

Template template = templateConfig.getTemplate("template.ftl");

Now i tried so many values for templateLoaderPath property but i always got the "Template not found." exception.

Freemarker module's JAR is like below

template.ftl
applicationContext-freemarker-module.xml
com/.../ (classes)
META-INF

Where should i put the template files and what should i set for templateLoaderPath value? I could not understand why "template.ftl" can not be found. I am trying to set the right value for many hours. I tried various path configurations without success.

Thanks a lot for your help,

jiraiya
  • 229
  • 2
  • 6
  • 12

2 Answers2

11

Make sure you have the following

  1. In your *-action servlet xml FreeMarkerConfigurationFactoryBean configuration has the "preferFileSystemAccess" property set to "false"

  2. <property name="templateLoaderPath" value="classpath*:/"/> should be <property name="templateLoaderPath" value="classpath:/"/>

    In freemarker the template loader tries to match a string "classpath:" , not "classpath*:"

  3. you have the JAR file under WEB-INF/lib folder.

  4. Finally, your template file under root of the jar file.

Jay
  • 701
  • 7
  • 19
  • Slightly different case for me: I had "classpath:templates" and it wasn't working on Jetty, changing it to "classpath:templates/" and it works fine! – dannrob Aug 28 '14 at 21:39
  • hello Jay, If my templates are under the dependency jar How can I load the templates . I use classpath*:/mailer_templates but It doesn`t work ): – Felix Mar 14 '15 at 02:45
  • @Felix: Check the second point mentioned above. classpath should not have a *. – Jay Mar 15 '15 at 03:42
3

Use some bean like this:

<bean
    class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="classpath:META-INF/freemarker/" />
    <property name="preferFileSystemAccess" value="false" />
</bean>

Hope this help you.

Defrag
  • 146
  • 1
  • 5