Thymeleaf概述
1,Thymeleaf是一个现代服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本。它的主要目标是提供一种优雅且高度可维护的模板创建方式。
Thymeleaf的基本语法说明
0,创建项目引入thymeleaf的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1,使用Thymeleaf 标签 之前需要引入<html xmlns:th="http://www.thymeleaf.org">。
如:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
</head>
<body>
<p th:text="#{home.welcome}">Welcome to our grocery store!</p>
</body>
</html>
2,如果我们希望获取到有未转义的数据使用:( th:utext对于“未转义的文本”),
例如:<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
结果:<p>Welcome to our <b>fantastic</b> grocery store!</p>
3,<p>Today is: <span th:text="${today}">13 February 2011</span></p>
当${today}中存在数值的时候,会替代"13 February 2011"值。
4,消息和变量表达式:如下
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
<p>Today is: <span th:text="${today}">13 february 2011</span></p>
5,准表达式功能:
简单表达:
变量表达式: ${...}
选择变量表达式: *{...}
消息表达式: #{...}
链接网址表达式: @{...}
片段表达式: ~{...}
字面:
文本:'one text','Another one!',...
数字:0,34,3.0,12.3,...
布尔:true,false
空字符: null
文字标记:one,sometext,main,...
文字操作:
字符串连接: +
字面替换: |The name is ${name}|
算术运算:
二元运算符:+,-,*,/,%
减号(一元运算符): -
布尔运算:
二元运算符:and,or
布尔否定(一元运算符): !,not
比较和平等:
比较:>,<,>=,<=(gt,lt,ge,le)<