一.HTML模板
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Index Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="${message}">Welcome to BeiJing!</p>
</body>
</html>
二.thymeleaf标准表达式
${...}
变量表达式*{...}
选择变量表达式#{...}
消息表达式@{...}
链接url表达式
三.thymeleaf属性使用
1.th:text 和 th:utext
<!-- th:text 为Thymeleaf属性,用于展示纯文本,会对特殊字符进行转义-->
<!-- 经过thymeleaf解析后,会将原来的内容进行替换 -->
<div th:text="hello">world</div>
<!-- th:utext 文本替换,不转义特殊字符 -->
<div th:utext="hello" >Hi</div>
th:text和th:utext的异同:
相同:
1.都可以对变量或表达式进行求值
<div id="thymeleaf-id"></div>
2.用“ + ”可进行文本连接
不同:
当获取从后段传来的参数带有html标签时 th:text不会进行解析 th:utext进行解析
2.th:id
替换id属性
<div id="id1" th:id="thymeleaf-id"></div>
页面解析后:
<div id="thymeleaf-id"></div>
3. th:value
替换value属性
<input type="text" value="input" th:value="thymeleaf_value"/>
4.th:object
{}选择表达式选取值 若父标签没有选择对象,字标签使用*{}选择表达式或${}变量表达式效果是一样的, 同时父标签选择了对象,字标签仍可用${}变量表达式取值
<div th:object="${user}">
第一:<span th:text="*{username}"></span>
</div>
<div>
第二:<span th:text="*{user.username}"></span>
</div>
<div th:object="${user}">
第三:<span th:text="${user.username}"></span>
</div>
解析后:
第一:张三
第二:张三
第三:张三th:with
5.th:with
局部变量赋值运算
<span th:with="a = 'hello world'" th:text="${a}"></span>
四.传参方式
thymeleaf采用form表单向后台提交数据
<form class="logonmain" action="users/login" method="POST">
<input name='sex'/>
<input type='sunmit'> //提交表单
</form>