thymeleaf

原文链接:https://www.tianmaying.com/tutorial/using-thymeleaf

模板
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<!--/*@thymesVar id="name" type="java.lang.String"*/-->
<p th:text="'Hello!, ' + ${name} + '!'" >3333</p>
</body>
</html>


一.

1.
<html xmlns:th="http://www.thymeleaf.org">
总结: 这是使用thymesVar语法的前提

2.
<p th:text="'Hello!, ' + ${name} + '!'" >3333</p>
总结: 获取变量的值:${} 和el一样。但是这个必须放在th:标签里使用

3.
<a th:href="@{http://blog.csdn.net/u012706811}">绝对路径</a>
<a th:href="@{/}">相对路径</a>
<a th:href="@{css/bootstrap.min.css}">Content路径,默认访问static下的css文件夹</a>

总结:Thymeleaf对于URL的处理是通过语法@{…}来处理的 类似的标签有:th:href和th:src

4.字符串替换:
两种方法:
(1)<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
(2)<span th:text="|Welcome to our application, ${user.name}!|">

总结:这种形式限制比较多,|…|中只能包含变量表达式${…},不能包含其他常量、条件表达式等


二.

1.Thymeleaf中使用th:if和th:unless属性进行条件判断

<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
总结: th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。

2.Thymeleaf同样支持多路选择Switch结构:
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>


3.遍历
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

原文地址:https://www.cnblogs.com/tian666/p/thymeleaf.html