1.1 th:text
例如<span th:text="Hello"></span>
如果是变量,要确保后台返回了该属性的变量,不然页面会有异常
<span th:text="${msg}"></span>
1.2 th:value
<input type="text" name="userName" th:value="${user.userName}"/>
1.3判断字符串是否为空
Thymeleaf 内置对象,注意语法:
1.调用内置对象一定要用#;
2.大部分的内置对象都以 s 结尾 strings、numbers、dates
判断字符串是否为空,如果为空返回true,否则返回false
${#strings.isEmpty(key)}
判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false
${#strings.contains(msg,'T')}
判断当前字符串是否以子串开头,如果是返回 true,否则返回 false
${#strings.startsWith(msg,'a')}
判断当前字符串是否以子串结尾,如果是返回 true,否则返回 false
${#strings.endsWith(msg,'a')}
返回字符串长度
${#strings.length(msg)}
查找子串的位置,并返回该子串的下标,如果没找到则返回-1
${#strings.indexOf(msg,'h')}
截取子串,用户与 jdk String 类下 SubString 方法相同
${#strings.substring(msg,13)}
${#strings.substring(msg,13,15)}
字符串转大小写
${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)}
1.4 th:attr
1.4 th:attr
<span>
<a class="deleteUser" th:attr="userid=${user.id},username=${user.userName}"
href="#"><img src="/img/schu.png" alt="删除" title="删除"/>
</a>
</span>
2.日期格式化处理
格式化日期,默认的以浏览器默认语言为格式化标准
${#dates.format(key)}
按照自定义的格式做日期转换
${#dates.format(key,'yyy/MM/dd')}
year:取年,Month:取月,Day:取日
${#dates.year(key)}
${#dates.month(key)}
${#dates.day(key)}
<p>出生日期:
<input type="text" name="birthday" class="Wdate" onfocus="WdatePicker()"
th:value="${#dates.format(user.birthday,'yyyy-MM-dd')}"/>
</p>
3.条件判断
3.1 th:if
方式一:
<span th:if="${sex} == ' 男 '">
性别:男
</span>
<span th:if="${sex} == ' 女 '">
性别:女
</span>
方式二:
<td><span th:if="${user.gender eq 1}">男</span>
<span th:if="${user.gender eq 2}">女</span>
</td>
3.2 th:switch
<div th:switch="${id}">
<span th:case="1">ID 为 1</span>
<span th:case="2">ID 为 2</span>
<span th:case="3">ID 为 3</span>
</div>
4.迭代遍历
4.1 th:each
<table border="1">
</tr>
<tr th:each="u : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
</tr>
</table>
<span>用户角色:
<select name="queryUserRole">
<option value="0">--请选择--</option>
<option
th:each="role :${roleList}"
th:value="${role.id}" th:text="${role.roleName}"
th:selected="${queryUserRole eq role.id}">
</option>
</select>
</span>
4.2 th:each 状态变量
@RequestMapping("/show3")
public String showInfo3(Model model){
List<Users> list = new ArrayList<>();
list.add(new Users(1,"张三",20));
list.add(new Users(2,"李四",22));
list.add(new Users(3,"王五",24));
model.addAttribute("list", list);
return "index3";
}
<table border="1">
<tr th:each="u,var : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
<td th:text="${var.index}"></td>
<td th:text="${var.count}"></td>
<td th:text="${var.size}"></td>
<td th:text="${var.even}"></td>
<td th:text="${var.odd}"></td>
<td th:text="${var.first}"></td>
<td th:text="${var.last}"></td>
</tr>
</table>
状态变量属性1,index:当前迭代器的索引 从 0 开始2,count:当前迭代对象的计数 从 1 开始3,size:被迭代对象的长度4,even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始5,first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false6,last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false
4.3 th:each 迭代 Map
@RequestMapping("/show4")
public String showInfo4(Model model){
Map<String, Users> map = new HashMap<>();
map.put("u1", new Users(1,"张三",20));
map.put("u2", new Users(2,"李四",22));
map.put("u3", new Users(3,"王五",24));
model.addAttribute("map", map);
return "index4";
}
<table border="1">
<tr th:each="maps : ${map}">
<td th:text="${maps}"></td>
</tr>
</table>
<table border="1">
<tr th:each="maps : ${map}">
<td th:each="entry:${maps}"
th:text="${entry.value.userid}" ></td>
<td th:each="entry:${maps}"
th:text="${entry.value.username}"></td>
<td th:each="entry:${maps}"
th:text="${entry.value.userage}"></td>
</tr>
</table>
5 域对象操作
5.1 HttpServletRequest
request.setAttribute("req", "HttpServletRequest");
Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span><br/>
5.2 HttpSession
request.getSession().getServletContext().setAttribute("app","Application");
Application:<span th:text="${application.app}"></span>
示例:
欢迎:<span th:text="${session.userSession.userName}"></span></h2>
6 url表达式
th:href
th:src
6.1 表达式语法
基本语法:@{}
示例:
<a class="modifyUser" th:href="@{'/modifyUser.do/'+${user.id}}">
<img src="/img/xiugai.png" alt="修改" title="修改"/>
</a>
分页代码:
<div style="margin-top:10px;">
<a th:href="@{'javascript:goPage(1)'}">首页</a>
<a th:href="@{'javascript:goPage('+${pageIndex-1}+')'}">上一页</a>
<a style="margin: 3px"
th:if="${totalPageCount gt 0}"
th:each="i:${#numbers.sequence(1,totalPageCount)}"
th:href="@{'javascript:goPage('+${i}+')'}"
th:text="${i}">
</a>
<a th:href="@{'javascript:goPage('+${pageIndex+1}+')'}">下一页</a>
<a href="#" th:onclick="'javascript:goPage('+${totalPageCount}+')'">尾页</a>
第<input type="text" style="width:40px" id="index" name="index"/>页
<input type="button" value="确定" onclick="goPage(document.getElementById('index').value)"></button>
</div>
6.2 URL 类型
绝对路径
<a th:href="@{http://www.baidu.com}">绝对路径</a><br/>
<a th:href="@{/show}">相对路径</a>
<a th:href = "@{'http://localhost:8080/viewUser.do?id='+${user.id}}">绝对路径</a>
<a th:href="@{'http://localhost:8080/view/'+${user.id}}">查看</a> ##restful传参
<a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>
相对路径
相对于服务器路径的根
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
6.3 button的click
<button id = "ddd" th:onclick="show([[${user.id}]])">查看</button>