允许 Apache 的 FreeMarker 模板引擎从外部资源文件夹加载模板
Allow Apache's FreeMarker template engine to load templates from outside Resources folder
我使用 Spring Initilizr 创建了一个 Spring 启动应用程序。然后我包含了 Apache's Freemarker
模板引擎来从我的项目中加载模板。引擎默认从以下位置加载模板:src/main/resources/templates/
文件夹。
我正在尝试在用户访问网页 http://localhost:8080
时加载一个简单的 index.ftl
文件作为模板。但我需要从 src/main/webapp/
文件夹加载模板。每当我尝试从 resources
文件夹外部加载模板时,模板引擎找不到模板。
我已经阅读了各种教程和 Stack Overflow 问题。 None 回答我的问题,我被 404 ERROR
困住了,因为引擎无法找到文件。
文件结构为:
|-src
|---main
|-----java
|-------MainApplication.java
|-------controllers
|---------ViewController.java
|-----resources
|-------static
|-------templates
|-------application.properties
|-----webapp
|-------index.ftl
经过大量挖掘,我遇到了 post 他们建议更改模板引擎搜索文件的位置。它建议在 application.properties 中添加以下行:
spring.freemarker.enabled=true
spring.freemarker.template-loader-path=classpath:src/main/webapp/
这似乎根本不起作用。
当我访问位于 http://localhost:8080 的网页时,我正在尝试解析简单的索引页面。我在 ViewController.java
:
中编写了以下用于映射 HTTP 请求的代码
@RequestMapping("/")
public ModelAndView home(Model model)
{
return new ModelAndView("index");
}
不知道我是完全弄错了还是错过了一些配置。
编辑
在嵌入式中 tomcat 你可以在 application-properties
中定义静态资源路径
spring.mvc.static-path-pattern=
如果部署到tomcat,在tomcat中使用可以在server.xmlstatic context里面定义可以存放freemarker文件,如
<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.
来自 Spring 文档:
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
与 web archive 关联,将在您生成 war.
时由 maven war 插件打包
假设您需要一个单独的位置来保存 ftl 模板,并且您仍然希望打包为 jar,您可以按照以下步骤操作。
在构建的 pom 文件中添加资源条目,以便资源插件可以将该目录复制到类路径。
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resources>
<build>
更改加载程序路径以从类路径的 ROOT 读取。
spring.freemarker.template-loader-path=classpath:
如果它仅用于 ftl 模板,我会将目录更改为 src/main/ftls
以避免混淆并在资源中更新相同的目录。
更新
I actually wanted to build a WAR deployment package
您必须使用 war 插件来构建 war。添加插件,在pom.xml中将包装改为war。
我使用 Spring Initilizr 创建了一个 Spring 启动应用程序。然后我包含了 Apache's Freemarker
模板引擎来从我的项目中加载模板。引擎默认从以下位置加载模板:src/main/resources/templates/
文件夹。
我正在尝试在用户访问网页 http://localhost:8080
时加载一个简单的 index.ftl
文件作为模板。但我需要从 src/main/webapp/
文件夹加载模板。每当我尝试从 resources
文件夹外部加载模板时,模板引擎找不到模板。
我已经阅读了各种教程和 Stack Overflow 问题。 None 回答我的问题,我被 404 ERROR
困住了,因为引擎无法找到文件。
文件结构为:
|-src
|---main
|-----java
|-------MainApplication.java
|-------controllers
|---------ViewController.java
|-----resources
|-------static
|-------templates
|-------application.properties
|-----webapp
|-------index.ftl
经过大量挖掘,我遇到了 post 他们建议更改模板引擎搜索文件的位置。它建议在 application.properties 中添加以下行:
spring.freemarker.enabled=true
spring.freemarker.template-loader-path=classpath:src/main/webapp/
这似乎根本不起作用。
当我访问位于 http://localhost:8080 的网页时,我正在尝试解析简单的索引页面。我在 ViewController.java
:
@RequestMapping("/")
public ModelAndView home(Model model)
{
return new ModelAndView("index");
}
不知道我是完全弄错了还是错过了一些配置。
编辑
在嵌入式中 tomcat 你可以在 application-properties
中定义静态资源路径 spring.mvc.static-path-pattern=
如果部署到tomcat,在tomcat中使用可以在server.xmlstatic context里面定义可以存放freemarker文件,如
<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.
来自 Spring 文档:
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
与 web archive 关联,将在您生成 war.
假设您需要一个单独的位置来保存 ftl 模板,并且您仍然希望打包为 jar,您可以按照以下步骤操作。
在构建的 pom 文件中添加资源条目,以便资源插件可以将该目录复制到类路径。
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resources>
<build>
更改加载程序路径以从类路径的 ROOT 读取。
spring.freemarker.template-loader-path=classpath:
如果它仅用于 ftl 模板,我会将目录更改为 src/main/ftls
以避免混淆并在资源中更新相同的目录。
更新
I actually wanted to build a WAR deployment package
您必须使用 war 插件来构建 war。添加插件,在pom.xml中将包装改为war。