首页 归档 关于 文件 Github
×

SpringBoot2.3.x 整合 Thymeleaf

2020-08-13 16:12:17
SpringBoot
  • Thymeleaf
本文总阅读量(次):
本文字数统计(字):462
本文阅读时长(分):2

依赖

1
2
3
4
5
6
7
8
9
10
<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-thymeleaf</artifactId>
</dependency>

Thymeleaf (主推)
轻量级的模板引擎(负责逻辑业务的不推荐,解析DOM或者XML会占用多的内存)
可以直接在浏览器中打开且正确显示模板页面
直接是html结尾,直接编辑

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
spring:
application:
name: spring-boot-thymeleaf
jackson:
default-property-inclusion: non_null
# 配置模版引擎, 是否开启thymeleaf缓存,本地为false,生产建议为true
thymeleaf:
# 是否启用模板缓存,开发时关闭缓存,不然没法看到实时页面
cache: false
# 是否检查模板位置是否存在。
check-template: true
# 是否为Web框架启用Thymeleaf视图分辨率。
enabled: true
# 编码格式, 默认UTF-8
encoding: UTF-8
# 应用于模板的模板模式。另请参阅Thymeleaf的TemplateMode枚举。
mode: HTML
servlet:
content-type: text/html
# 后缀 默认 .html
suffix: .html
# 模板文件存放位置 , 默认 classpath:/templates/
prefix: classpath:/templates/
1
2
3
4
src/main/java/com/demon/freemarker/controller
IndexController.java
src/main/resources/templates
index2.html

示例代码

IndexController.java

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

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf</title>
</head>
<body>
this is welcome <span th:text="${content}"></span>
</body>
</html>

运行示例

20200813175610

完
SpringBoot2.3.x整合Freemarker
MyCat 分库配置

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

文章作者:十二

发布时间:2020-08-13 16:12:17

最后更新:2021-07-02 11:24:58

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

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

头像

十二

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

分类

  • Blog4
  • ElasticSearch13
  • Freemarker2
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx3
  • Kafka4
  • Linux2
  • Logger5
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot11
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 二月 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

作品

我的微信 我的文件

网站信息

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