当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
![[Camp Experience Post] 2022 Cybersecurity Summer Camp](/img/1e/716bafc679dc67d3d54bcc21a3b670.png)
[Camp Experience Post] 2022 Cybersecurity Summer Camp

如何进行数据库备份

ICLR 2022最佳论文:基于对比消歧的偏标签学习

软件测试之移动APP安全测试简析,北京第三方软件检测机构分享

Appears in oozie on CDH's hue, error submitting Coordinator My Schedule

洞见云原生微服务及微服务架构浅析

Classical Literature Reading--DLO
![[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](/img/63/16de443caf28644d79dc6e6889e5dd.png)
[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

仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)

TexturePacker使用文档
随机推荐
使用Ganache、web3.js和remix在私有链上部署并调用合约
2022 6th Strong Net Cup Part WP
几道关于golang并发的面试题
洞见云原生微服务及微服务架构浅析
cdh6 opens oozieWeb page, Oozie web console is disabled.
@Transactional注解在类上还是接口上使用,哪种方式更好?
Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
chrome copies the base64 data of an image
The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
一款简洁的文件传输工具
C语言——分支语句和循环语句
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
正则表达式
@Scheduled注解详解
Avoid , ,
, and tags获取小猪民宿(短租)数据
检查点是否在矩形内
Quartus 使用 tcl 文件快速配置管脚
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
Docker搭建Mysql主从复制