首页 归档 关于 文件 Github
×

SpringBoot2.3.x整合Freemarker

2020-08-13 16:58:50
SpringBoot
  • freemarker
本文总阅读量(次):
本文字数统计(字):586
本文阅读时长(分):2

Freemarker
FreeMarker Template Language(FTL) 文件一般保存为 xxx.ftl
严格依赖MVC模式,不依赖Servlet容器(不占用JVM内存)
内建函数

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 将freemarker的html内容写入生成的文件中 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

配置

yml内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
spring:
application:
name: spring-boot-freemarker
jackson:
default-property-inclusion: non_null
# 配置模版引擎
freemarker:
cache: false #是否启用模板缓存
enabled: true #是否启用freemarker
template-loader-path: classpath:/templates/ #设定模板的加载路径,多个以逗号分隔
suffix: .ftl #设定模板的后缀
content-type: text/html
check-template-location: true #是否检查模板位置是否存在
charset: UTF-8 #模板编码
#一些常用配置
allow-request-override: false #是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性
allow-session-override: false #是否允许HttpSession属性覆盖(隐藏)控制器生成的同名模型属性
expose-request-attributes: false #设定所有request的属性在merge到模板的时候,是否要都添加到model中
expose-session-attributes: false #是否在merge模板的时候,将HttpSession属性都添加到model中
expose-spring-macro-helpers: true #设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
prefer-file-system-access: true #是否优先从文件系统加载template,以支持热加载,默认为true

数据应用

☞传送门

页面使用

文件路径:

1
2
3
4
src/main/java/com/demon/freemarker/controller
IndexController.java
src/main/resources/templates
index1.ftl

示例代码

IndexController.java

@Controller
public class IndexController {
    /**
     * 路由 /index1
     * 返回 index1 这里默认配置自动映射到目录(templages/)下的index1.ftl
     */
    @GetMapping("/index1")
    public String index(Model model){
        model.addAttribute("content","hello freemarker");
        return "index1";
    }
}

index.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemarker</title>
</head>
<body>
this is welcome ${content}
</body>
</html>

运行示例

20200813175553

更多

完
Nginx 服务器证书SSL安装
SpringBoot2.3.x 整合 Thymeleaf

本文标题:SpringBoot2.3.x整合Freemarker

文章作者:十二

发布时间:2020-08-13 16:58:50

最后更新:2025-06-05 17:52:35

原始链接:https://www.zhuqiaolun.com/2020/08/1597309130215/1597309130215/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx6
  • Kafka4
  • Linux2
  • Logger6
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot16
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 一月 20261
  • 十二月 20253
  • 八月 20252
  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:103.2k
本站文章数量统计:139
© 2026 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github