Shivam Naik
Shivam Naik

Reputation: 115

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.

Upvotes: 6

Views: 2609

Answers (2)

s7vr
s7vr

Reputation: 75914

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

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58774

EDIT

In embedded tomcat you can define static resources path in application-properties

 spring.mvc.static-path-pattern=

If you deploy to tomcat, in tomcat use can define inside server.xml static context that can hold freemarker files, as

 <Context docBase="/home/stuff" path="/static" />

A <Context> element is added inside the <Host> element. Context has two attributes: docBase is the directory on disk that contains your static files and path is the URL that you want to serve the files on.

Upvotes: 0

Related Questions