Thymeleaf的语法详解
字符串操作,日期转换
<span th:text="hello"></span><hr/>
<span th:text="${msg}"/><hr/>
判断字符串是否为空:
<span th:text="${#strings.isEmpty(msg)}"/><hr/>
判断字符串是否含有某个字符:
<span th:text="${#strings.contains(msg,'x')}"/><hr/>
判断字符串是否含以某个字符开头:
<span th:text="${#strings.startsWith(msg,'一')}"/><hr/>
判断字符串是否含以某个字符结尾:
<span th:text="${#strings.endsWith(msg,'mlxg')}"/><hr/>
查看字符串的长度:
<span th:text="${#strings.length(msg)}"/><hr/>
返回指定字符串的位置:
<span th:text="${#strings.indexOf(msg,'中')}"/><hr/>
返回截取指定的字符串:
<!--(和String的substring用法一样)-->
<span th:text="${#strings.substring(msg,7)}"/>
<span th:text="${#strings.substring(msg,5,9)}"/><hr/>
返回字符串的大小写:
<span th:text="${#strings.toUpperCase(msg)}"/>
<span th:text="${#strings.toLowerCase(msg)}"/><hr/>
当前日期时间:
<span th:text="${#dates.format(now)}"/><hr/>
更改当前日期时间格式:
<span th:text="${#dates.format(now,'yyy/MM/dd HH时 mm分 ss秒')}"/><hr/>
获取当前的年:
<span th:text="${#dates.year(now)}"/><hr/>
获取当前的月:
<span th:text="${#dates.month(now)}"/><hr/>
获取当前的日:
<span th:text="${#dates.day(now)}"/><hr/>
获取当前的小时:
<span th:text="${#dates.hour(now)}"/><hr/>
获取当前的分:
<span th:text="${#dates.minute(now)}"/><hr/>
获取当前的秒:
<span th:text="${#dates.second(now)}"/><hr/>
条件判断,迭代遍历,获取作用域对象数据
if语句:
<span th:if="${sex}=='男'" >
性别:男
</span>
<span th:if="${sex}=='女'" >
性别:女
</span><hr/>
switch(List集合)语句:
<span th:switch="${id}">
<span th:case="1">id=1</span>
<span th:case="2">id=2</span>
<span th:case="3">id=3</span>
</span><hr/>
each迭代语句:
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Index</th>
<th>Count</th>
<th>Size</th>
<th>Even</th>
<th>Odd</th>
<th>First</th>
<th>Last</th>
</tr>
<tr th:each="u,var:${users}">
<th th:text="${u.uid}"></th>
<th th:text="${u.uname}"></th>
<th th:text="${u.age}"></th>
<th th:text="${var.index}"></th>
<th th:text="${var.count}"></th>
<th th:text="${var.size}"></th>
<th th:text="${var.even}"></th>
<th th:text="${var.odd}"></th>
<th th:text="${var.first}"></th>
<th th:text="${var.last}"></th>
</tr>
</table><hr/>
each(map集合)迭代
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="map:${map}">
<th th:each="entry:${map}" th:text="${entry.value.uid}"></th>
<th th:each="entry:${map}" th:text="${entry.value.uname}"></th>
<th th:each="entry:${map}" th:text="${entry.value.age}"></th>
</tr>
</table><hr/>
获取作用域数据:<br/>
Requset:<span th:text="${#httpServletRequest.getAttribute('req')}" ></span><br/>
Session:<span th:text="${session.session}" ></span><br/>
Application:<span th:text="${application.context}" ></span><hr/>
URL表达式
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="http://www.baidu.com">绝对路径--href</a><hr/>
<a th:href="@{http://www.baidu.com}">绝对路径--th:href</a><hr/>
<!--在url中实现参数传递-->
<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}">相对路径-传参--restful</a>
<!--在url中通过restful风格进行参数传递-->
<a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>
<!--相对于服务器路径的根-->
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
<!--相对于当前项目的根相对于项目的上下文的相对路径-->
<a th:href="@{/show}">相对路径</a>
</body>
</html>

浙公网安备 33010602011771号