SpringBoot系列——Thymeleaf模板

SpringBoot系列——Thymeleaf模板

一.Thymeleaf介绍

​ 1.Thymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语言体系下有模板引擎,在C#、PHP语言体系下也有模板引擎,甚至在 Javascript中也会用到模板引擎技术,Java生态下的模板引擎有 Thymeleaf、 Freemaker、Ⅴ elocity、 Beetl(国产)等。

​ 2. Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。它与 JSP,Velocity,FreeMaker 等模板引擎类似,也可以轻易地与 Spring MVC 等 Web 框架集成。与其它模板引擎相比,Thymeleaf 最大的特点是,即使不启动 Web 应用,也可以直接在浏览器中打开并正确显示模板页面。

​ 3.Thymeleaf特点:

​ Thymeleaf是比jsp功能更加强大,效率更快的模板
​ SpringBoot集成了Thymeleaf模板
​ Thymeleaf模板是基于HTML的,以HTML标签为载体

二.基础设置

1.Thymeleaf常用设置

在SpringBoot的application配置文件中
在Controller层写路径时可以省略前后缀
字符串连接可以用双||
运算符:
gt——>
lt——<
ge——>=
le——<=
==——eq
!=——ne

2.模板引擎的缓存机制
2.1一般在开发阶段关闭,在发布阶段开启:
spring.thymeleaf.cache=false

​ 2.2编码格式(默认是utf-8):
​ spring.thymeleaf.encoding=utf-8

​ 2.3模板类型(默认是HTML):
​ spring.thymeleaf.mode=HTML

​ 2.4模板的前缀,默认是resources下的templates
​ spring.thymeleaf.prefix=classpath:/templates/

​ 2.5模板的后缀,默认是.html
​ spring.thymeleaf.suffix=.html

三.Thymeleaf使用(属性)

1.在HTML的html标签中加入引用xmlns:th=“http://www.thymeleaf.org”

2.依赖:

<dependency>  
<groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

3.标准变量表达式-获取key对应的作用域数据

​ 语法格式:th:text=“${key}”

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    商品页面
    <b style="color: red;" th:text="${msg}"></b>
    <br/>
<table style="width: 100%;">
    <thead>
        <tr>
            <th>编号</th>
            <th>型号</th>
            <th>内存</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="p : ${phoneList}">
            <th th:text="${p.phoneId}"></th>
            <th th:text="${p.modelNumber}"></th>
            <th th:text="${p.capactiy}"></th>
        </tr>
    </tbody>
</table>
</body>
</html>

4.选择变量表达式

语法:*{key}
作用:获取key对应的作用域对象,,需要和th:object一起使用
注:*{key}需要在th:object="${key}的标签子标签内

5.链接表达式

语法:@{url}
作用:表示超链接

6.th:each循环

语法:th:each

7.if判断

语法:th:if=“boolean条件”
作用:如果表达式为真,则显示标签内容
语法:th:unless=“boolean条件”
作用:如果表达式为假,则显示签内容

8.switch判断

语法:th:switch="${key}" th:case=“值”
作用:显示case相等的

9.th:inline内联

语法:th:inline=“text”
语法:th:inline=“javascript”
作用:可以直接用内联内容显示表达式结果

四.Thymeleaf使用(对象)

request表示HttpServletRequest对象
session表示HttpSession对象
session表示HttpSession对象

语法:${#request.key}

五.Thymeleaf自定义模板(内容复用)

​ 定义模板语法:th:fragment=“模板自定义名称”
​ 引用模板语法:{模板的文件名称 : :自定义模板名称}
​ 第二种格式:模板的文件名称 : :自定义模板名称

​ 模板的使用:包含模板(th:include),插入模板(th:insert)

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:fragment="top">
    <p>自定义模板</p>
    <p>www.localhost.com</p>
</div>
</body>
</html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:insert="~{ head :: top }">
    insert方式使用模板
</div>
<div th:insert="head :: top">
    insert方式使用模板
</div>
<div th:include="~{ head :: top }">
    include方式
</div>
<div th:include="head :: top">
    include方式
</div>
<div th:include="footer :: html">
    整个html文件当做模板使用
</div>
<div th:include="footer">
    整个html文件当做模板使用
</div>
<div th:include="fragment :: html">
    使用其他目录中的模板
</div>
<div th:include="fragment">
    使用其他目录中的模板
</div>
</body>
</html>

六.总结

  • 简洁易读:Thymeleaf使用自然的HTML标记作为模板,不需要特殊的语法标记,使得模板文件更加易读、易于维护

  • 强大的表达式:Thymeleaf支持使用表达式输出变量值、执行方法等操作,使得模板可以动态渲染数据

  • 条件语句和循环语句:Thymeleaf提供了方便的条件语句和循环语句,可以根据条件显示不同的内容,或者循环遍历集合、数组等数据

  • 模板继承:Thymeleaf支持模板继承,可以通过定义基础模板和子模板的方式实现页面的重用和模块化开发

  • 国际化支持:Thymeleaf内置了国际化功能,可以根据用户的语言环境自动切换不同的文本资源

  • 安全防护:Thymeleaf提供了对HTML转义的支持,可以防止XSS攻击

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值