小编Shi*_*aik的帖子

允许Apache的FreeMarker模板引擎从Resources文件夹外部加载模板

我使用Spring Initilizr创建了一个Spring Boot Application。然后,我包括Apache's Freemarker模板引擎以从我的项目中加载模板。默认情况下,引擎从以下位置加载模板:src/main/resources/templates/文件夹。

我正在尝试index.ftl在用户访问网页时加载一个简单的文件作为模板http://localhost:8080。但是我需要从src/main/webapp/文件夹加载模板。每当我尝试从resources文件夹外部加载模板时,模板引擎都无法找到模板。

我经历了各种教程和堆栈溢出问题。没有人回答我的问题,我被困住了,404 ERROR因为引擎无法找到文件。

文件结构为:

|-src
|---main
|-----java
|-------MainApplication.java
|-------controllers
|---------ViewController.java
|-----resources
|-------static
|-------templates
|-------application.properties
|-----webapp
|-------index.ftl
Run Code Online (Sandbox Code Playgroud)

经过大量的挖掘,我遇到了一个帖子,他们建议更改模板引擎搜索文件的位置。建议在application.properties中添加以下行:

spring.freemarker.enabled=true
spring.freemarker.template-loader-path=classpath:src/main/webapp/
Run Code Online (Sandbox Code Playgroud)

这似乎根本不起作用。

当我访问位于http:// localhost:8080的网页时,我试图解析简单的索引页。我已经编写了以下代码来映射HTTP请求ViewController.java

@RequestMapping("/")
public ModelAndView home(Model model)
{
    return new ModelAndView("index");
}
Run Code Online (Sandbox Code Playgroud)

不知道我是完全搞错了还是错过了一些配置。

java freemarker spring-boot

6
推荐指数
1
解决办法
316
查看次数

标签 统计

freemarker ×1

java ×1

spring-boot ×1