- Thymeleaf介绍
- Thymeleaf常用设置
- Thymeleaf使用
- 标准变量表达式
- 选择变量表达式
- 链接表达式
- Thymeleaf属性
- th:each循环
- if判断
- switch判断
- th:inline内联
- Thymeleaf基本对象
- #request对象/#session对象/session对象
- Thymeleaf内置工具类
- #dates
- #numbers
- #strings
- #lists
- null处理
- Thymeleaf自定义模板(内容复用)
Thymeleaf常用设置Thymeleaf是比jsp功能更加强大,效率更快的模板
SpringBoot集成了Thymeleaf模板
Thymeleaf模板是基于HTML的,以HTML标签为载体
Thymeleaf官网:Thymeleaf
Thymeleaf手册:Thymeleaf
在SpringBoot的application配置文件中
在Controller层写路径时可以省略前后缀
字符串连接可以用双||
运算符:
gt——>
lt——<
ge——>=
le——<=
==——eq
!=——ne
#模板引擎的缓存机制 #一般在开发阶段关闭,在发布阶段开启 spring.thymeleaf.cache=false #编码格式(默认是utf-8) spring.thymeleaf.encoding=utf-8 #模板类型(默认是HTML) spring.thymeleaf.mode=HTML #模板的前缀,默认是resources下的templates spring.thymeleaf.prefix=classpath:/templates/ #模板的后缀,默认是.html spring.thymeleaf.suffix=.htmlThymeleaf使用
在HTML的html标签中加入引用xmlns:th=“http://www.thymeleaf.org”
thymeleaf模板起步依赖
标准变量表达式org.springframework.boot spring-boot-starter-thymeleaf
语法格式:th:text="${key}"
作用:获取key对应的作用域数据
Title
对象属性
id
name
sex
选择变量表达式
语法:{key}
作用:获取key对应的作用域对象,但不能单独用,需要和th:object一起使用
注:{key}需要在th:object="${key}的标签子标签内
Title
对象属性
id
name
sex
链接表达式
语法:@{url}
作用:表示超链接
Title
绝对路径
相对地址
相对地址带参数
传参数方式
相对地址带参数
Thymeleaf属性
Thymeleaf属性是原HTML原属性加th即可
Title
th:each循环
语法:th:each
Title
if判断
语法:th:if=“boolean条件”
作用:如果表达式为真,则显示标签内容
语法:th:unless=“boolean条件”
作用:如果表达式为假,则显示标签内容
Title
第一个div
unless
switch判断
语法:th:switch="${key}" th:case=“值”
作用:显示case相等的
Title
显示zs
显示ls
显示ww
th:inline内联
语法:th:inline=“text”
语法:th:inline=“javascript”
作用:可以直接用内联内容显示表达式结果
Title
我是[[${user.name}]]
Thymeleaf基本对象
#request对象/#session对象/session对象#request表示HttpServletRequest对象
#session表示HttpSession对象
session表示HttpSession对象
语法:${#request.key}
Title
Thymeleaf内置工具类
#dates
Title
#numbers
Title
#strings
${#strings.toString(obj)}
${#strings.isEmpty(name)}
${#strings.arrayIsEmpty(nameArr)}
${#strings.listIsEmpty(nameList)}
${#strings.setIsEmpty(nameSet)}
${#strings.indexOf(name,frag)}
${#strings.substring(name,3,5)}
${#strings.substringAfter(name,prefix)}
${#strings.substringBefore(name,suffix)}
${#strings.replace(name,'las','ler')}
#lists
${#lists.toList(object)}
${#lists.size(list)}
${#lists.isEmpty(list)}
${#lists.contains(list, element)}
${#lists.containsAll(list, elements)}
${#lists.sort(list)}
null处理
Title
Thymeleaf自定义模板(内容复用)
定义模板语法:th:fragment=“模板自定义名称”
引用模板语法:~{模板的文件名称 :: 自定义模板名称}
第二种格式:模板的文件名称 :: 自定义模板名称
模板的使用:包含模板(th:include),插入模板(th:insert)
Title
自定义模板
www.localhost.com
Title
insert方式使用模板
insert方式使用模板
include方式
include方式
整个html文件当做模板使用
整个html文件当做模板使用
使用其他目录中的模板
使用其他目录中的模板



