Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Freemarker Template files from src/main/resources folder?

How to access my freemarker template (*.ftl) files stored within my src/main/resources folder from my code (Spring Boot application) ?

I tried the following

freemarker.template.Configuration config = new Configuration(); configuration.setClassForTemplateLoading(this.getClass(), "/resources/templates/"); 

and getting the following exception

freemarker.template.TemplateNotFoundException: Template not found for name "my-template.ftl". 
like image 267
yathirigan Avatar asked Jun 29 '15 13:06

yathirigan


2 Answers

The root of the classpath is src/main/resources, change the path to

configuration.setClassForTemplateLoading(this.getClass(), "/templates/"); 
like image 115
Nathan Hughes Avatar answered Oct 19 '22 12:10

Nathan Hughes


I was facing "freemarker.template.TemplateNotFoundException: Template not found for name..." issue. My code was correct but I forgot to include /templates/ directory in pom.xml. So below code fixed the issue for me. I hope this helps.

 AppConfig.java :      @Bean(name="freemarkerConfiguration")     public freemarker.template.Configuration getFreeMarkerConfiguration() {         freemarker.template.Configuration config = new freemarker.template.Configuration(freemarker.template.Configuration.getVersion());         config.setClassForTemplateLoading(this.getClass(), "/templates/");         return config;     }   EmailSenderServiceImpl.java:      @Service("emailService")     public class EmailSenderServiceImpl implements EmailSenderService      {         @Autowired         private Configuration freemarkerConfiguration;          public String geFreeMarkerTemplateContent(Map<String, Object> dataModel, String templateName)         {             StringBuffer content = new StringBuffer();             try {                 content.append(FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(templateName), dataModel));                 return content.toString();             }             catch(Exception exception) {                 logger.error("Exception occured while processing freeMarker template: {} ", exception.getMessage(), exception);             }             return "";         }     }    pom.xml :      <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-freemarker</artifactId>         </dependency>          <dependency>             <groupId>org.freemarker</groupId>             <artifactId>freemarker</artifactId>             <scope>compile</scope>         </dependency>     </dependencies>      <build>         <resources>             <resource>                 <directory>src/main/resources/</directory>                 <includes>                     <include>templates/*.ftl</include>                 </includes>             </resource>              <resource>                 <directory>src/main/</directory>                 <includes>                     <include>templates/*.ftl</include>                 </includes>             </resource>         </resources>      </build>   
like image 22
Ved Singh Avatar answered Oct 19 '22 10:10

Ved Singh