创建项目
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 https://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.ljw</groupId>
<artifactId>SpringBootThymeleaf02</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>
<!-- springboot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
创建存放视图目录
src/main/resource/templates
templates:该目录是安全的,该目录下面的内容是不允许外界直接访问的
thymeleaf的特点
Thymeleaf是通过特定语法对html的标记做渲染
thymeleaf基础操作
1,变量输出和字符操作
th:text
在页面当中输出某个值
th:value
可以将一个值放入到input标签的value中
th:field可以回写用于更新现实
th:field="${user.name}"
判断字符串内容是否为空
thymeleaf内置对象,完成对字符串的操作
注意语法:
1,调用内置对象一定要用#
2,大部分的内置对象都以s结尾,如:strings,numbers,dates
${#strings.isEmpty(key)}
判断字符串是否为空,如果为空返回true,否则返回false
${#strings.contains(msg,'T')}
判断字符串是否包含指定的子串,如果包含返回true,否则返回false
${#strings.startsWith(msg,'a')}
判断当前字符串是否以子串开头,如果是返回他true,否则返回false
${#strings.endsWith(msg,'a')}
判断当前字符串是否以子串结尾,如果是返回他true,否则返回false
${#strings.length(msg)}
返回字符串的长度
${#strings.indexOf(msg,'h')}
查找子串的位置,并返回该子串的下标,如果没找到就返回-1
${#strings.substring(msg,1)}
${#strings.substring(msg,1,2)}
截取子串,用法跟jdk String类下的SubString方法相同
${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)}
字符串大小写
2,日期格式化处理
${#dates.format(date)}
格式化日期,默认以浏览器默认语言为格式化标准
${#dates.format(date,'yyyy/MM/dd')}
按照自定义的格式做日期转化
${#dates.year(date)}
${#dates.month(date)}
${#dates.day(date)}
取年月日
3,条件判断
th:if
<samp th:if="${sex} == '男'">性别:男</samp>
<samp th:if="${sex} == '女'">性别:女</samp>
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,迭代遍历
th:each
list迭代遍历
<table border="1" >
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
<tr th:each="u : ${user}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
</tr>
</table>
map对象迭代遍历
<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>
th:each状态变量属性
index当前迭代器的索引 从0开始
count当前迭代对象的计数 从1开始
size被迭代对象的大小
even\odd:布尔值,当前循坏是否是偶数\奇数
first返回布尔值,当前循坏是否是第一条,如果是返回true,否则返回false
last返回布尔值,当前循坏是否最后一条,如果是返回true,否则返回false
<tr th:each="u,var : ${user}">
<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>
5,域对象操作
HttpServletRequest
HttpSession
ServletContext
@RequestMapping("/show5")
public String showInfo5(HttpServletRequest request,Model model) {
request.setAttribute("req", "HttpServletRequest");
request.getSession().setAttribute("sess", "HttpSession");
request.getSession().getServletContext().setAttribute("app", "Application");
return "index5";
}
request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span><br/>
session:<span th:text="${session.sess}"></span><br/>
application:<span th:text="${application.app}"></span>
6,URL表达式
th:href
th:src
url表达式语法
基本语法:@{}
url类型
绝对路径
<a th:href="@{http://www.baidu.com}">绝对路径1</a><br/>
相对路径
1,相对于当前项目的根
相对于项目的上下文的相对路径
<a th:href="@{/show}">相对路径</a>
2,相对于当前服务器路径的根
<a th:href="@{~/projectName/resourceName}">相对于服务器的根</a>
3,在url中实现参数传递
<a th:href="@{/show(id=1,name=shangsan)}">相对路径-传参数</a>
4,在URL中通过restful风格进行参数传递
<a th:href="@{/path/{id}/show(id=1,name=shangsan)}">相对路径-传参数-restful风格</a>