thymeleaf语法
PDF文档地址:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf
1.导入名称空间
xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org"
注:不导入也没事,写代码没有提示。
2.属性
2.1."th:属性" 修改html中的属性值
<div class="div1" th:class="div2" id="divid1" th:id="divid2" th:text="2222222">111111111</div>
2.2.自定义属性"th:arrt"使用
<div data-id="100" data-name="100" th:attr="(data-id=200,data-name=200)">1000</div>
2.3.行内写法
[[]]==text==转义文本
[()]==utext==不转义文本
转义文本
<div th:text="${hello}"></div>
<div>[[${hello}]]</div>
<hr/>
不转义文本
<div th:utext="${hello}"></div>
<div>[(${hello})]</div>
5.表达式
表达式语法:
获取变量值(OGNL表达式): ${...}
1)获取对象属性,调用方法
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.
如:{session.user}
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).
如:${#strings.isEmpty(name)}
选择表达式: *{...}
补充:配合th:object=使用
<div th:object="${session.user}">
<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>
国际化内容: #{...}
自定义URL: @{...}
使用时不需要用问号(?)方式来写如:
<a href="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
片段引用表达式: ~{...}
字面量:
字符串: 'one text' , 'Another one!' ,…
数字: 0 , 34 , 3.0 , 12.3 ,…
布尔值: true , false
空值: null
数组逗号隔开: one , sometext , main ,…
文本操作:
字符串拼接: +
字符串替换: |The name is ${name}|
数学运算:
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
布尔运算:
Binary operators: and , or
Boolean negation (unary operator): ! , not
比较运算:
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
条件运算:
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
特殊操作:
No-Operation: _
6.使用
HelloController.java
package cn.luoruiyuan.login;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class HelloController {
@RequestMapping("/success")
public String success(Map<String,Object> map){
map.put("hello","<h2>Hello H2</h2>");
ArrayList<String> user=new ArrayList<>();
user.add("admin");
user.add("admin1");
user.add("admin2");
user.add("admin3");
map.put("user",user);
Map<String,String> addr=new HashMap<>();
addr.put("admin","SY");
addr.put("admin1","CD");
addr.put("admin2","GZ");
addr.put("admin3","SH");
List<Map<String,String>> list=new ArrayList<>();
list.add(addr);
list.add(addr);
list.add(addr);
map.put("addr",addr);
map.put("addrList",list);
return "success";
}
}
success.html
<!DOCTYPE>
<html lang="en" html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
转义文本
<div th:text="${hello}"></div>
<div>[[${hello}]]</div>
<hr/>
不转义文本
<div th:utext="${hello}"></div>
<div>[(${hello})]</div>
<hr/>
选择表达式使用
<ul th:object="${addr}">
<li th:text="*{admin}"></li>
<li th:text="*{admin1}"></li>
<li th:text="*{admin2}"></li>
<li th:text="*{admin3}"></li>
</ul>
<hr/>
循环用户
<ul>
<li th:each="u:${user}" th:text="${u}"></li>
</ul>
循环地址
<ul>
<li th:each="al:${addrList}" th:text="${al.admin}"></li>
</ul>
</body>
</html>
结果