当前位置:网站首页>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:switchth: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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 切面打印调取的方法
- The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
- 20220725资料更新
- A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
- Additional Features for Scripting
- 程序员如何优雅地解决线上问题?
- The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
- 6132. All the elements in the array is equal to zero - quick sort method
- Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
- Sql之各种Join
猜你喜欢

GIF制作-灰常简单的一键动图工具

The monthly salary of the test post is 5-9k, how to increase the salary to 25k?

企业防护墙管理,有什么防火墙管理工具?

cdh6 opens oozieWeb page, Oozie web console is disabled.

访问控制台中的选定节点

cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed

2022 6th Strong Net Cup Part WP
![[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环](/img/63/16de443caf28644d79dc6e6889e5dd.png)
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环

A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing

chrome copies the base64 data of an image
随机推荐
Spark Sql之join on and和where
drf生成序列化类代码
Sql之各种Join
2022 6th Strong Net Cup Part WP
Architecture basic concept and nature of architecture
Chapter 12 End-User Task As Shell Scripts
@WebServlet注解(Servlet注解)
UML diagram of soft skills
IDEA common plugins
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
检查点是否在矩形内
洞见云原生微服务及微服务架构浅析
problem solved
辛普森悖论
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
What can be done to make this SQL into a dangerous SQL?
FAST-LIO2代码解析(二)
UI自动化测试框架搭建-标记性能较差用例
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~
The Spark of Sql join on the and and where