一、为什么要使用thymeleaf?
- jsp执行过程: jsp-->servlet. java-->serv1et. class-->servlet (out. print('html')),速度慢.
- thymeleaf能生成静态页面,直接交给浏览器执行,速度快。
1.编写pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>10-spring-boot-view-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 修改版本 -->
<properties>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
2.创建存放视图的目录
目录位置:src/main/resources/templates
templates:该目录是安全的。意味着该目录下的内容是不允许外界直接访问的。
Thymeleaf 特点
Thymelaef 是通过他特定语法对 html 的标记做渲染。
编写 Controller
package com.bjsxt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Thymeleaf入门案例
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model model) {
model.addAttribute("msg", "Thymeleaf 第一个案例");
return "index";
}
}
创建视图 .html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Thymeleleaf 入门</title>
</head>
<body>
<span th:text="hello"></span><hr/>
<span th:text="${msg}"></span><hr/>
</body>
</html>
编写启动类
package com.bjsxt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Thymeleaf 语法详解
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Thymeleleaf 入门</title>
</head>
<body>
<span th:text="hello"></span>
<hr />
<span th:text="${msg}"></span>
<hr />
<input type="text" name="" th:value="${msg}"></input>
<!-- 判断字符串是否为空,如果为空返回 true,否则返回 false -->
<span th:text="${#strings.isEmpty(msg)}"></span>
<hr />
<!-- 判断当前字符串是否包含此字符串开头 返回 True或False -->
<span th:text="${#strings.contains(msg,'T')}"></span>
<hr />
<span th:text="${#strings.contains(msg,'t')}"></span>
<hr />
<!-- 判断当前字符串是否以字符串开头返回 True或False -->
<span th:text="${#strings.startsWith(msg,'a')}"></span>
<hr />
<span th:text="${#strings.startsWith(msg,'T')}"></span>
<hr />
<!-- 判断当前字符串是否以字符串结尾,返回 True或False -->
<span th:text="${#strings.endsWith(msg,'a')}"></span>
<hr />
<span th:text="${#strings.endsWith(msg,'案例')}"></span>
<hr />
<!-- 获取字符串的长度 -->
<span th:text="${#strings.length(msg)}"></span>
<hr />
<!-- 查找子串位置,返回索引下标,从0开始,没找到返回-1 -->
<span th:text="${#strings.indexOf(msg,'h')}"></span>
<hr />
<!-- 截取字符串:一个参数从此处开始截取到最后,两个是开始待结束 -->
<span th:text="${#strings.substring(msg,10)}"></span>
<hr />
<span th:text="${#strings.substring(msg,10,12)}"></span>
<hr />
<!-- 转大写 -->
<span th:text="${#strings.toUpperCase(msg)}"></span>
<hr />
<!-- 转小写 -->
<span th:text="${#strings.toLowerCase(msg)}"></span>
<hr />
<!-- 格式化日期,默认以浏览器的默认语言为格式化标准 -->
<span th:text="${#dates.format(key)}"></span>
<hr />
<!-- 按照给定格式转化日期 -->
<span th:text="${#dates.format(key,'yyyy/MM/dd')}"></span>
<hr/>
<!-- 去年、月、日 -->
<span th:text="${#dates.year(key)}"></span>
<span th:text="${#dates.month(key)}"></span>
<span th:text="${#dates.day(key)}"></span>
</body>
</html>
条件判断
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--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>
</body>
</html>
迭代遍历 th:each
controller
@RequestMapping("/show3")
public String showInfo3(Model model) {
List<Users> list=new ArrayList<Users>();
list.add(new Users(1, "张三", 15));
list.add(new Users(2, "李四", 15));
list.add(new Users(3, "王五", 15));
model.addAttribute("list", list);
return "index3";
}
}
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" >
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<tr th:each="u,var : ${list}">
<th th:text="${u.userid}"></th>
<th th:text="${u.username}"></th>
<th th:text="${u.userage}"></th>
<!--状态变量属性 -->
<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>
</body>
</html>
状态变量属性
- index:当前迭代器的索引 从 0 开始
- count:当前迭代对象的计数 从 1 开始
- size:被迭代对象的长度
- even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始
- first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false
- last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false
th:each 迭代 Map
controller
@RequestMapping("/show4")
public String showInfo4(Model model) {
Map<String, Users> map=new HashMap<String, Users>();
map.put("1", new Users(1,"张三", 15));
map.put("2", new Users(2,"李四", 15));
map.put("3", new Users(3,"王五", 15));
model.addAttribute("map", map);
return "index4";
}
index4.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<tr th:each="maps : ${map}">
<th th:text="${maps.value.userid}"></th>
<th th:text="${maps.value.username}"></th>
<th th:text="${maps.value.userage}"></th>
</tr>
</table>
</body>
</html>
--------------------------------------------------------------------------
Thymeleaf域对象操作
controller
@Controller
public class DemoController {
@RequestMapping("/show5")
public String showInfo5(HttpServletRequest request,Model model) {
request.setAttribute("key", "HttpServletRequest");
request.getSession().setAttribute("key", "session");
request.getSession().getServletContext().setAttribute("key", "Application");
return "index5";
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--HttpServletRequest作用域-->
Request:<span th:text="${#httpServletRequest.getAttribute('key')}"></span>
<!--Session作用域-->
Session:<span th:text="${session.key}"></span>
<!--ServletContext作用域-->
Application:<span th:text="${application.key}"></span>
</body>
</html>
------------------------------------------------------------------------
URL 表达式
基本语法:@{}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Thymeleleaf 入门</title>
</head>
<body>
<a th:href="@{http://www.baidu.com}">绝对路径</a>
<a href="http://www.baidu.com">绝对路径2</a>
<hr/>
<!-- 相当于当前项目的根 -->
<a th:href="@{/show}">show</a>
<hr/>
<!-- 相当于服务器(tomcat)的根 -->
<a th:href="@{~/}">相当于服务器的根</a>
<hr/>
<!--在 url 中实现参数传递-->
<a th:href="@{/show(id=1,name=zhangshan)}">相对路径,传参</a>
<!--在 url 中通过 restful 风格进行参数传递-->
<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}">相对路径-传参-restful</a>
</body>
</html>
---
pagecontroller
package com.bjsxt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
*/
@Controller
public class DemoController {
@RequestMapping("/{page}")
public String showInfo(@PathVariable String page) {
return page;
}
}