Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow Apache's FreeMarker template engine to load templates from outside Resources folder

I created a Spring Boot Application using Spring Initilizr. I then included Apache's Freemarker template engine to load the templates from my project. The engine by default loads templates from: src/main/resources/templates/ folder.

I am trying to load a simple index.ftl file as template when the user visits the webpage http://localhost:8080. But I need to load the templates from src/main/webapp/ folder. Whenever I try to load the templates from outside the resources folder, the template engine fails to find the templates.

I have gone through various tutorials and Stack Overflow questions. None answer my question and I'm stuck with a 404 ERROR because the engine is not able to find the files.

The file structure is:

|-src
|---main
|-----java
|-------MainApplication.java
|-------controllers
|---------ViewController.java
|-----resources
|-------static
|-------templates
|-------application.properties
|-----webapp
|-------index.ftl

After a lot of digging, I came across a post where they suggested changing the location where the template engine searches the files. It suggested adding following lines in application.properties:

spring.freemarker.enabled=true
spring.freemarker.template-loader-path=classpath:src/main/webapp/

This doesn't seem to work at all.

I am trying to resolve simple index page when I visit the webpage at http://localhost:8080. I've written following code for mapping the HTTP request in ViewController.java:

@RequestMapping("/")
public ModelAndView home(Model model)
{
    return new ModelAndView("index");
}

No idea if I am totally getting it wrong or I've missed some configuration.

like image 686
Shivam Naik Avatar asked Jul 09 '19 19:07

Shivam Naik


1 Answers

From Spring docs:

Do not use the src/main/webapp directory if your application is packaged as a jar. Although this directory is a common standard, it works only with war packaging, and it is silently ignored by most build tools when you generate a war.

src/main/webapp is associated with web archive and will be packaged by maven war plugin when you generate the war.

Assuming you require a separate location to keep ftl templates and you would still like to package as jar you could follow below steps.

Add the resource entry in the build in pom file so resource plugin can copy that directory to classpath.

<build>
    <resources>
      <resource>
         <directory>src/main/webapp</directory>
     </resource>
    <resources>
<build>

Change the loader path to read from the ROOT of classpath.

spring.freemarker.template-loader-path=classpath:

If its only for ftl templates I would change the directory to src/main/ftls to avoid confusion and update the same in the resources.

UPDATE

I actually wanted to build a WAR deployment package

You have to use war plugin to build war. Add the plugin and change the packaging to war in pom.

More about the Traditional Deployment: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html#howto-create-a-deployable-war-file

like image 150
s7vr Avatar answered Oct 19 '22 15:10

s7vr