SpringBoot引入freemarker模板

x33g5p2x  于2021-03-14 发布在 Spring  
字(1.4k)|赞(0)|评价(0)|浏览(252)

1. 添加如下依赖
`pom.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. `application.properties`配置freemarker相关配置**
`resources/application.properties

````xml
# 指定freemarker的后缀名
# freemarker默认的文件后缀为 .ftl,但是也可以指定为 .html
spring.freemarker.suffix=.html

```
**3. 模板引擎默认应该存放在`resources/templates/`目录**
**4. 添加`.html`为后缀的freemarker模板**
如果你在`application.properties`中指定了freemarker模板的后缀名为`.html`,为了支持freemarker的语法,在IDEA添加`*.html`:
File >> Settings >>
![](http://img.saoniuhuo.com/images/202108/91451629902622684.jpg)
**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>
    <script src="../static/index.js"></script>
</head>
<body>
    <h1>欢迎使用freemarkder</h1>
</body>
</html>

```
完成上面的过程就可以使用freemarker来编写前端页面了。

相关文章

微信公众号

最新文章

更多