Thymeleaf的常用标签
取值
<!--不能用request.xxx的方式-->
reqeuset:
<span th:text="${userName}"></span>
<span th:text="${#httpServletRequest.getAttribute('userAge')}"></span><br>
session:
<span th:text="${session.session}"></span><br>
application:
<span th:text="${application.servletContext}"></span><br>
解析url地址
解析url地址:
<!--http://localhost:8080/test-->
<a th:href="@{/test}">test</a>
直接表达式
有转义效果:[[${xxx}]]
:会把<>等特殊字符用>等表示,不会被解析成标签
无转义效果:[(${xxx})]
:会直接被浏览器解析
条件判断
条件判断:
<p th:if="${not #strings.isEmpty(userName)}">if判断为真时才输出内容</p>
<p th:if="${#strings.isEmpty(userName)}">if判断为真时才输出内容</p><br>
</body>
遍历集合
遍历集合:
<!--th:each="声明变量 : 集合"-->
<!--th:each在哪个标签上,哪个标签就会出现多次-->
<div>
<p th:text="${userID}" th:each="userID:${userIdList}"></p>
</div>
包含片段
<html xmlns:th="http://www.thymeleaf.org">
<div th:fragment="myFirstPart">
this is be included html
</div>
<div th:fragment="mySecondPart">
this is be included html
</div>
<div th:fragment="myThirdPart">
this is be included html
</div>
包含片段:
<!--~{springboot指定前缀+目录结构+springboot指定后缀::代码片段的名字}-->
<div th:insert="~{include/common::myFirstPart}"></div><!--插入div-->
<div th:replace="~{include/common::mySecondPart}"></div><!--part的div替换了原有的div-->
<div th:include="~{include/common::myThirdPart}"></div><br><!--原有的div替换了part的div-->