SpringBoot2.x系列教程(三十一)Thymeleaf的基本使用

本篇文章通过实例为大家讲解Thymeleaf的基本使用。

赋值

基本赋值

<h1 th:text="${name}">默认值</h1>

字符串拼接

普通拼接形式:

<p th:text="'Hello ' + ${name}"></p>

简洁写法:

<p th:text="|Hello ${name}|"></p>

条件判断

条件判断有两种形式:th:if和th:unless。

th:if条件判断:

<a th:if="${isShow == 'true'}" th:href="@{https://www.choupangxia.com/}">官网</a>

th:unless条件判断:

<a th:unless="${isShow == 'no'}" th:href="@{https://www.choupangxia.com/}">官网</a>

这两种条件判断,顾名思义,判断方式恰好想法。if是满足条件显示,unless是其中的条件取反。

URL

上面示例中显示了URL的基本使用,Thymeleaf通过@{…}来语法来处理URL。如果需要Thymeleaf对URL进行渲染,那么务必使用th:href、th:src等属性。

<a th:href="@{https://www.choupangxia.com/(pageNum=${pageNum})}">官网</a>

<a href="details.html" th:href=

猜你喜欢

转载自blog.csdn.net/wo541075754/article/details/103976859