4.2日期格式化处理
${#dates.format(key)}(格式化日期)
${#dates.format(date,‘yyyy-MM-dd’)}(自定义格式)
${#dates.year(date)}(取年)
${#dates.month(date)}(取月)
${#dates.day(date)}(取日)
4.3条件判断
th:if
<span th:if="${sex} =='男'">
性别:男
</span>
<span th:if="${sex} =='女'">
性别:女
</span>
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迭代遍历
th:each
@RequestMapping("/show3")
public String show3(Model model) {
List<Users> list = new ArrayList<>();
list.add(new Users(01,"pp",22));
list.add(new Users(02,"pj",23));
list.add(new Users(03,"jj",24));
list.add(new Users(04,"jp",25));
model.addAttribute("list", list);
return "index3";
}
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<tr th:each="u:${list}">
<td th:text="${u.id}"></td>
<td th:text="${u.name}"></td>
<td th:text="${u.age}"></td>
</tr>
</table>
th:each的状态变量
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>index</th>
<th>size</th>
<th>count</th>
<th>even</th>
<th>odd</th>
<th>first</th>
<th>last</th>
</tr>
<tr th:each="u,var:${list}">
<td th:text="${u.id}"></td>
<td th:text="${u.name}"></td>
<td th:text="${u.age}"></td>
<td th:text="${var.index}">索引,从0开始</td>
<td th:text="${var.size}">大小</td>
<td th:text="${var.count}">计数</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>
th:each遍历map
<table border="2">
<tr th:each="m:${map}">
<td th:each="v:${m}" th:text="${v.value.id}"></td>
<td th:each="v:${m}" th:text="${v.value.name}"></td>
<td th:each="v:${m}" th:text="${v.value.age}"></td>
</tr>
</table>
4.5域对象操作
HttpServletRequest
HttpSession
ServletContext
@RequestMapping("/show5")
public String show5(HttpServletRequest request ) {
request.setAttribute("request", "----------------request");
request.getSession().setAttribute("session", "-------------session");
request.getSession().getServletContext().setAttribute("application","-----------application");
return "index5";
}
Request:<span th:text="${#httpServletRequest.getAttribute('request')}"></span><br/>
Session:<span th:text="${session.session}"></span><br/>
Application:<span th:text="${application.application}"></span
4.6URL表达式
表达式基本语法@{}
URL类型:
绝对路径:
<a th:href="@{http://www.baidu.com}">绝对路径</a>
相对路径:
相对于当前目录的根
<a th:href="@{/index5}">相对路径</a>
相对于服务器的相对路径
<a th:href="@{~/.../...}">相对路径</a>
在URL中实现参数的传递
<a th:href="@{/index5(id=1,name=pj)}">相对路径--传参</a>
@RequestMapping("/{page}")
public String show5(@PathVariable String page,Integer id,String name,HttpServletRequest request) {
request.setAttribute("id", id);
request.setAttribute("name", name);
return page;
}
id:<span th:text="${#httpServletRequest.getAttribute('id')}"></span>
name:<span th:text="${#httpServletRequest.getAttribute('name')}"></span>