当前位置:网站首页>Thymeleaf简介
Thymeleaf简介
2022-08-01 23:44:00 【bhegi_seg】
关于我第一次使用Thymeleaf写模板代码,被折磨这件事…
Thymeleaf官网
https://www.thymeleaf.org/
学习中自然少不了查阅文档,虽然这对于一个英语学渣来说是一件比较痛苦的事情,但是还是要说,既然要学习编程,大部分文档都是英文的,还是要努力克服一下,说不定时间长了就看懂了呢,还能顺带小小的提升一下自己的词汇量。
Thymeleaf模板引擎
Thymeleaf作为众多模板引擎中的一种,我最初也是对它充满了陌生,在我学习SpringBoot并且试图通过学习来搭建属于自己的个人博客网站时,前端代码是少不了的,选择Thymeleaf来写模板自然不是因为我学的有多么好,而是因为我选择的教程是用的Thymeleaf,而我又不会其他的…
当然,学习Thymeleaf之前还是需要掌握一点web的知识的,我认为有着web基础多多少少能为我们的Thymeleaf之路提供了一点便利,减少一丢丢的痛苦。
Thymeleaf模板文件后缀名就是.html
, 制作完成后,我们可以直接打开浏览效果
使用之前我们需要现在我们的SpringBoot项目中添加Thymeleaf的依赖
,即在pom.xml
文件中添加如下代码:
<!-- thymeleaf模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
我们的html文件也需要放置在template
的文件夹下,这样我们就可以开始使用Thymeleaf模板引擎了
还有一个存放静态资源的文件夹static
Thymeleaf简单示例
可以看到,当我们没有传递参数直接静态访问时,页面就会是下图中的效果
当时当我们传递参数后,页面中的内容就会被替换为我们传递的内容
常用标签
标签
作用
示例
th:text
替换文本
th:text="${title}"
th:value
替换值
th:value="${user.name}"
th:each
迭代
th:each="stu:${user}"
th:href
替换链接
th:href="@{/index.html}"
th:src
替换资源
th:src="@{/img/Wechat.jpg}"
th:if
条件判断
th:if="${isVip}"
th:switch
th:case
条件判断
th:switch="${sex}"
th:case="'man'"
th:insert
插入
th:insert="footer :: copy"
th:replace
替换
th:replace="footer :: copy"
th:fragment
定义片段
th:fragment="frag (onevar,twovar)"
在Thymeleaf 中,如果想引入链接比如link,href,src,需要使用@{资源地址}
引入资源
接下来便是我编写Thymeleaf页面的崎岖之旅
第一步:将html页面中的html
标签修改成<html xmlns:th="http://www.w3.org/1999/xhtml">
第二步:编写Thymeleaf页面,我就浅浅的编写一下吧,首先就是定义一个.html
的文件作为我们的Thymeleaf文件,我就命名为_thymeleaf
然后在我们的页面中写上如下代码
下面就是我们在请求test页面是所得到的效果,模板内的内容会被替换到我们指定的位置中
td:fragment
就是定义一个片段,然后使用 td:replace
将片段中的内容替换到指定位置
这只是一个简单的测试而已,我们也可以拓展应用,比如用来替换我们做网站时的导航栏,底部,这样不就是有一个简单的模板应用了,其他强大的功能,还是需要静心学习。
本篇内容就到这里吧,我们下次再见!
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Department project source code sharing
- 计算两点之间的中点
- Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
- Chapter 12 End-User Task As Shell Scripts
- 问题解决方式了
- 月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
- Chapter 11 Working with Dates and Times
- 递归:方法调用自身
- cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
- ELK日志采集
猜你喜欢
Flink Yarn Per Job - 提交流程一
洞见云原生微服务及微服务架构浅析
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
Appears in oozie on CDH's hue, error submitting Coordinator My Schedule
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
Docker搭建Mysql主从复制
1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
随机推荐
Spark Sql之union
numpy.around
Oracle database is set to read-only and read-write
GIF制作-灰常简单的一键动图工具
Docker搭建Mysql主从复制
Several interview questions about golang concurrency
架构基本概念和架构本质
类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
CDH6 Hue to open a "ASCII" codec can 't encode characters
ELK log collection
加载字体时避免隐藏文本
JAX-based activation function, softmax function and cross entropy function
YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
How to better understand and do a good job?
numpy.where
6133. Maximum number of packets
recursion: method calls itself
Flink学习第三天——一文带你了解什么是Flink流?
辛普森悖论
检查 Oracle 版本的 7 种方法