Thymeleaf - Java的模板引擎

Thymeleaf官网:https://www.thymeleaf.org/
文档齐全,不过都是English,这里提供一个中文版的:
链接:https://pan.baidu.com/s/1G4GLANy3CVbyxMsYcUDe9g
提取码:gwnv

概念:
  • Web和独立环境的开源的Java模板引擎
  • 能够处理HTML,XML,JavaScript,CSS甚至纯文本
  • 提供完整的SpringFramework集成
  • JSP的完全替代品

动态资源: JSP(spring boot默认不支持,也可通过配置使其支持)
推荐:模板引擎 thymeleaf
网页 = 模板+数据

环境配置:

官网查询 thymeleaf的依赖,即下载并导入:thymeleaf-x.x.x RELEASE.jar 包
Maven项目单独使用,导入如下依赖:

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.11.RELEASE</version>
</dependency>

SpringBoot项目导入如下依赖即可:

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

通过ThymeleafProperties源码得知:
在这里插入图片描述

使用thymeleaf只需要将 文件放入目录:“classpath:/templates/”;文件的后缀: “.html

基本语法使用

引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 

xmlns是xml namespace的缩写,也就是XML命名空间;在html中引入此命名空间,可避免编辑器出现html验证错误,虽然加不加命名空间对Thymeleaf的功能没有任何影响

<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

一个W3C的标准

github上的一个示例项⽬:Good Thymes Virtual Grocery:
https://github.com/thymeleaf/thymeleafexamples-gtvg

可参考官方文档和下例博客:
https://www.cnblogs.com/aland-1415/p/9383740.html

常用例子:

<!--时间格式化-->
th:text="${#dates.format(xxx.updateTime,'yyy-MM-dd HH:mm:ss')}"

<!--获取xxx数组元素个数-->
th:text="${#arrays.length(xxx)}"

<!--Thymeleaf会取代原标签内容,所以原标签内容写什么或不写均可 -->
<a href="/yyy" th:href="@{/yyy}" target="_blank">
<a href="/yyy/id" th:href="@{/yyy/{id}(id=${xxx.id})}" target="_blank">
<a href="/yyy/id/input" th:href="@{/yyy/{id}/input(id=${xxx.id})}">编辑</a>
<a href="/yyy?pageNum=(xxx-1)" th:href="@{/yyy(pageNum=${xxx.pageNum}-1)}">上一页</a>
<a href="/yyy/id?pageNum=(xxx+1)" th:href="@{/yyy/{id}(id=${activeTypeId},(pageNum=${xxx.pageNum}+1))}" >下一页</a>

<!--加载图片-->
<img src="../static/images/xxx.jpg" th:src="@{/images/xxx.jpg}" alt="静态加载">
<img src="../static/images/xxx.jpg" th:src="@{${xxx.ooo}}" alt="动态加载"> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值