Thymeleaf语法的基本使用


SpringBoot导入依赖

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

其他导入

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

然后需要在html页面的html标签中加入xmlns:th="http://www.thymeleaf.org"命名空间。

然后使用th:元素名替换html的元素,即可使用thymeleaf表达式。

th:任意html属性:替代原生属性的值

th:text :改变当前元素里面的文本内容。
例:

<div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}"></div>

在这里插入图片描述

表达式

Simple expressions(表达式语法)

1. Variable Expressions: ${…} //获取变量值,底层是OGNL表达式
  1. 获取对象属性、调用方法
    例:
    1. ${person.father.name}
    2. ${person.createCompleteName()}
  2. 使用内置的基本对象
    #ctx : the context object. 
    #vars: the context variables. 
    #locale : the context locale. 
    #request : (only in Web Contexts) the HttpServletRequest object. 
    #response : (only in Web Contexts) the HttpServletResponse object. 
    #session : (only in Web Contexts) the HttpSession object. 
    #servletContext : (only in Web Contexts) the ServletContext object.
    
    例:
    1. ${session.size()}
    2. ${#request.getParameter(‘foo’)}
  3. 内置的一些工具对象
    #execInfo : information about the template being processed. 
    #messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax. 
    #uris : methods for escaping parts of URLs/URIs
    #conversions : methods for executing the configured conversion service (if any). 
    #dates : methods for java.util.Date objects: formatting, component extraction, etc. 
    #calendars : analogous to #dates , but for java.util.Calendar objects. 
    #numbers : methods for formatting numeric objects. 
    #strings : methods for String objects: contains, startsWith, prepending/appending, etc. 
    #objects : methods for objects in general. 
    #bools : methods for boolean evaluation. 
    #arrays : methods for arrays. 
    #lists : methods for lists. 
    #sets : methods for sets. 
    #maps : methods for maps. 
    #aggregates : methods for creating aggregates on arrays or collections. 
    #ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
    
    例:
    1. ${#numbers.sequence(from,to)}
    2. ${#strings.toString(obj)}
2. Selection Variable Expressions: *{…} //选择表达式

和${}在功能上是一样的,但是比 ${}多一个功能,需要配合th:object使用。
例:

<div th:object="${session.user}"> 
	<!--此时的*{firstName}等同于${session.user.firstName}-->
	<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p> 
	<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p> 
	<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p> 
</div>

等同于

<div> 
	<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p> 
	<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p> 
	<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p> 
</div>
3. Message Expressions: #{…} //获取国际化内容
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

结果:

home.welcome=¡Bienvenido a nuestra tienda de comestibles!	//西班牙语
4. Link URL Expressions: @{…} //定义URL
<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) --> 
<!--使用@{}在url中传参时用()代替?,多个参数用,逗号分隔,并且在@{}中可以使用${}获取值-->
<a href="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a> 
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) --> 
<!--@{}中,最前面/代表当前项目下-->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
 <!-- Will produce '/gtvg/order/3/details' (plus rewriting) --> 
 <!--在@{}中可以直接用{变量名}获取值-->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
5. Fragment Expressions: ~{…} //片段引用表达式
<div th:insert="~{commons :: main}">...</div>

在这里插入图片描述
thymeleaf的三种引入功能片段属性:

  • th:insert :将公共片段整个插入到声明引入的元素中
  • th:replace :将声明引入的元素替换为公共片段
  • th:include :将被引入的片段的内容包含进这个标签中
    三种引入属性的区别
<!--抽取片段-->
<footer th:fragment="copy">
	&copy; 2011 The Good Thymes Virtual Grocery 
</footer>

<!--三种引入方式-->
<!--1-->
<div th:insert="footer :: copy"></div> 
<!--2-->
<div th:replace="footer :: copy"></div> 
<!--3-->
<div th:include="footer :: copy"></div> 

<!--效果-->
<!--1-->
<div> 
	<footer> 
		&copy; 2011 The Good Thymes Virtual Grocery 
	</footer> 
</div> 
<!--2-->
<footer>
	 &copy; 2011 The Good Thymes Virtual Grocery 
</footer> 
<!--3-->
 <div> 
 	&copy; 2011 The Good Thymes Virtual Grocery 
</div>

Literals (字面量)

1. Text literals(字符串): ‘one text’ , ‘Another one!’ ,…
2. Number literals(数字): 0 , 34 , 3.0 , 12.3 ,…
3. Boolean literals(布尔值): true , false
4. Null literal(null值): null
5. Literal tokens(多数据 ,逗号分隔): one , sometext , main ,…

Text operations(文本操作)

1. String concatenation: +
2. Literal substitutions: |The name is ${name}|

Arithmetic operations(数学运算)

1. Binary operators: + , - , * , / , %
2. Minus sign (unary operator): -

Boolean operations(布尔运算)

1. Binary operators: and , or
2. Boolean negation (unary operator): ! , not

Comparisons and equality(比较运算)

1. Comparators: > , < , >= , <= ( gt , lt , ge , le )
2. Equality operators: == , != ( eq , ne )

Conditional operators(条件运算,三元运算符)

1. If-then: (if) ? (then)
2. If-then-else: (if) ? (then) : (else)
3. Default: (value) ?: (defaultvalue)

Special tokens(特殊操作,即_代表不操作)

1. No-Operation: _

标签内写法

如果想在标签内直接使用变量,需要遵循特定的语法。

<p>Hello, [[${session.user.name}]]!</p>

等同于

<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>

[[]]:如同th:text

例:

<p>The message is "[[${msg}]]"</p>

等同于

<p>The message is "This is &lt;b&gt;great!&lt;/b&gt;"</p>

[()]:如同th:utext

例:

<p>The message is "[(${msg})]"</p>

等同于

<p>The message is "This is <b>great!</b>"</p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thymeleaf是一个流行的服务器端Java模板引擎,它提供了一套丰富的语法和标签,让开发者可以更加方便地处理HTML模板。以下是Thymeleaf语法大全: 1. 基本语法 Thymeleaf基本语法使用${}表示表达式,例如: ``` <p th:text="${user.name}">John Doe</p> ``` 上面的代码会将user对象中的name属性的值显示在页面上。 2. 属性设置 Thymeleaf可以在HTML标签中添加属性,例如: ``` <img src="image.png" th:src="@{${user.avatarUrl}}" /> ``` 上面的代码中,使用@{}包裹表达式,将${user.avatarUrl}的值设置为img标签的src属性。 3. 条件判断 Thymeleaf支持if/else条件判断语句,例如: ``` <p th:if="${user.isAdmin}">Welcome Admin</p> ``` 上面的代码中,如果user.isAdmin为true,则显示“Welcome Admin”。 4. 列表循环 Thymeleaf可以通过th:each指令实现列表循环,例如: ``` <ul> <li th:each="item : ${items}" th:text="${item}"></li> </ul> ``` 上面的代码中,将items列表中的每个元素显示在li标签中。 5. 模板继承 Thymeleaf支持模板继承,可以在父模板中定义公共部分,例如: 父模板: ``` <html> <head th:fragment="head"> <title>My Website</title> <link href="style.css" rel="stylesheet" /> </head> <body> <header th:fragment="header"> <h1>My Website</h1> </header> <div th:fragment="content"></div> <footer th:fragment="footer"></footer> </body> </html> ``` 子模板: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" th:include="layout/layout :: head"> <body> <div th:include="layout/layout :: header"></div> <div th:replace="content"></div> <div th:include="layout/layout :: footer"></div> </body> </html> ``` 上面的代码中,子模板使用th:include指令引入父模板的公共部分,使用th:replace指令替换content内容。 6. URL处理 Thymeleaf可以处理URL,例如: ``` <a th:href="@{http://www.example.com}">Example Website</a> ``` 上面的代码中,将链接指向http://www.example.com。 以上是Thymeleaf语法大全,希望能帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值