企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
引入 Freemarker 模板的步骤如下: **1. 引入Freemarker依赖** ```xml <!-- 引入freemarker场景启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- 引入热重载组件,对于静态资源的访问尤为重要 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> ``` **2. `resources/application.properties`** ```properties spring: freemarker: suffix: .html #指定freemarker模板的后缀名,freemarker默认的文件后缀为 .ftl,但是也可以指定为 .html cache: false #关闭页面缓存,否则开发时看不到页面变化 ``` **3. 模板文件默认存放在`resources/templates/`目录** **4. 添加`.html`为后缀的freemarker模板** 如果你在`application.properties`中指定了 Freemarker 模板的后缀名为`.html`,为了支持 Freemarker 的语法,在 IDEA 添加`*.html`。 File -> Settings -> ![](https://img.kancloud.cn/3b/41/3b410c9563e5313aa9bf2b9684833151_1224x558.png) **5. 在模板文件添加freemarker的头部说明** *`resources/templates/index.html`* ```html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta charset="UTF-8"> <title>Index</title> </head> <body> <h1>欢迎使用freemarker</h1> </body> </html> ``` **6. controller层** ```java @Controller public class IndexController { @RequestMapping(value = "/index") public String index() { //会自动找到templates/index.html文件 return "index"; } } ``` **7. 测试** 启动项目后访问 http://localhost:8080/index ,界面将看到如下信息。 ```html 欢迎使用freemarker ``` ****