Thymeleaf是一个用于web和独立环境的现代服务器端Java模板引擎。
Thymeleaf是用来替换传统的jsp,做项目的展示的,SpringBoot里面并不推荐使用jsp了,推荐使用Thymeleaf,Thymeleaf可以运行在有网络的环境里,同时它也可以用在无网络的外部环境里,而有一些模板必须需要服务器才能运行。美工在开发的时候只能改html,我们在开发的时候得把html变成jsp,Thymeleaf可以运行在服务器里面,也可以本地运行,它既可以让美工在浏览器查看页面的静态效果,也可以让开发人员在服务器端看动态数据渲染之后的效果。
Thymeleaf是一个java的模板引擎,是用来显示数据的,Thymeleaf有一个要求,不能直接通过请求访问到Thymeleaf,Thymeleaf在用的时候,要访问Thymeleaf,必须得经过controller,由controller跳转到Thymeleaf,它才能得到一个正确的显示。
那么如何集成thymeleaf模板呢
用到的包结构:
引入依赖
org.springframework.boot spring-boot-starter-thymeleaf
编写配置
把Thymeleaf相关的模板都放到项目中的templates这个目录,css、js等静态资源放在static这个目录里面
需要在配置文件里告诉它模板在resources/templates里面,
指定thymeleaf模板目录和指定模板的后缀是默认的,不在配置文件里写也是可以的
spring:
thymeleaf:
prefix: classpath:/templates/ # 指定thymeleaf模板前缀目录
suffix: .html # 指定模板的后缀
cache: false # 是否开启thymeleaf缓存,默认为true是开启的,在开发过程中建议 # 关了
注:不使用缓存时我们还得设置一下,使themeleaf模板的修改随时生效
开发html页面并导入thymeleaf的头表示这是一个thymeleaf模板
index.html
springboot 之 thymeleaf 使用
thymeleaf 入门案例
开发的thymeleaf需要放在templates包里
使用时必须在页面中加入thymeleaf命名空间(导入thymeleaf的头)
将
修改为
ns表示namespace命名空间,冒号后面的th表示当前命名空间的名字,名字可以随便写,不过后面用的时候都得使用这个名字去使用,一般都写为th
开发controller
HelloController
package com.baizhi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("hello")
public class HelloController {
@RequestMapping("hello")
public String hello(){
System.out.println("hello ok...");
return "index";
}
}
如果直接在地址栏里访问thymeleaf的话thymeleaf语法是不生效的,所以我们需要先访问controller,由controller跳转到thymeleaf这样的话thymeleaf语法才生效,跳转时的语法和原来是一样的。
forward跳转:
return “index”;
当访问hello/hello的时候,它会返回一个视图,叫index,同时会经过视图解析器,在classpath:/templates/找一个index.html
测试访问
注:如果直接访问项目名 http://localhost:8989/springboot_day7/ 不访问controller中的方法那么默认访问项目中的index.html
在使用html作为thymeleaf模板时,必须将
修改为
传递数据
//forward跳转: request、model 作用域传递数据 //redirect跳转: session作用域传递数据 //传递单个(基本类型)数据 String Integer //传递对象类型数据 //传递集合类型数据 //如何在页面有条件展示数据 判断
获取request作用域中的数据
获取单个(基本类型)数据
获取接以文本形式输出
直接以文本形式输出
在controller的方法中传递数据
String name = "小陈";
Integer age = 23;
request.setAttribute("name", name);
request.setAttribute("age", age);
在themeleaf模板里获取数据
获取单个数据:
获取单个数据:
获取的是一段html标签
controller中传递数据:
String content = "百度一下";
request.setAttribute("content", content);
thymeleaf模板中获取数据:
直接以原样(html标签形式)输出
th:text="${content}"
获取单个数据:
如果想要先解析获取到数据之后想要先解析,而不是直接以文本形式输出
th:utext="${content}"
获取单个数据:
补充
如果想让作用域中的数据赋值给表单元素,想让标签的value是controller方法作用域获取的
th这个命名空间除了可以写th:text以外,它还可以加在html标签的任意属性上
另外:span标签可以写成自闭和标签,也可以写成围堵标签
# 总结
- 1.使用 th:text="${属性名}" 获取对应数据,获取数据时会将对应标签中数据清空,因此最好是空标签
- 2.使用 th:utext="${属性名}" 获取对应的数据,可以将数据中html先解析在渲染到页面
- 3.使用 th:value="${属性名}" 获取数据直接作为表单元素value属性
获取对象类型数据
controller的方法中传递数据:
User user = new User(21, "小张", 2300.23, new Date());
request.setAttribute("user", user);
themeleaf模板中获取数据:
获取对象类型数据: id: name: salary: birthday:
日期格式化
birthday:
集合类型数据
controller的方法中传递数据:
// 传递集合类型数据 Listusers = Arrays.asList(new User(22, "小黄", 232.2, new Date()), new User(23, "小王", 232.2, new Date()), new User(24, "win7", 232.2, new Date())); request.setAttribute("users", users);
themeleaf模板中获取数据:遍历集合
只有一个变量:每次遍历到的元素
- id: name: salary: birthday: birthday格式化:
两个变量:每次遍历到的元素,状态
- - 获取遍历次数 count 从1开始 index 从0开始
- - 获取当前遍历是否是奇数行
- - 获取当前遍历是否是偶数行
- - 获取当前集合的总条数
有条件的展示数据
controller方法中将数据放入request作用域
Integer age = 23;
request.setAttribute("age", age);
themeleaf模板中获取数据:
有条件的展示数据:
我是rebeccapurple我是green# 运算符 gt:great than(大于)> ge:great equal(大于等于)>= eq:equal(等于)== lt:less than(小于)< le:less equal(小于等于)<= ne:not equal(不等于)!=
而age=23,所以应该输出 “我是rebeccapurple”
获取session作用域中的数据
获取session中数据使用session.key
controller的方法中将数据放入session作用域:
// 向session作用域存储数据
session.setAttribute("username", "小二黑");
themeleaf中获取数据:
获取session作用域中数据:
获取application作用域中的数据
获取application中的数据:application.key
获取application作用域中的数据需要在数据名字前面加上 application.
测试
资源类User
public class User {
private Integer id;
private String name;
private Double salary;
private Date birthday;
// 提供有参、无参构造方法,get、set方法
// 以免影响阅读,这里没有粘,但其实是有的
}
DemoController
package com.baizhi.controller;
import com.baizhi.eneity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("demo")
public class DemoController {
//forward跳转: request、model 作用域传递数据
//redirect跳转: session作用域传递数据
//传递单个(基本类型)数据 String Integer
//传递对象类型数据
//传递集合类型数据
//如何在页面有条件展示数据 判断
@RequestMapping("demo")
public String demo(HttpServletRequest request, HttpSession session){
System.out.println("demo ok");
// 传递单个(基本)类型数据
String name = "小陈";
Integer age = 23;
String content = "百度一下";
request.setAttribute("name", name);
request.setAttribute("age", age);
request.setAttribute("content", content);
// 传递对象类型数据
User user = new User(21, "小张", 2300.23, new Date());
request.setAttribute("user", user);
// 传递集合类型数据
List users = Arrays.asList(new User(22, "小黄", 232.2, new Date()),
new User(23, "小王", 232.2, new Date()),
new User(24, "win7", 232.2, new Date()));
request.setAttribute("users", users);
// 向session作用域存储数据
session.setAttribute("username", "小二黑");
return "demo";
}
}
demo.html
用来测试thymeleaf语法
thymeleaf语法基本使用
获取单个数据:
获取单个数据:
获取单个数据:
获取对象类型数据:
id:
name:
salary:
birthday:
获取集合类型数据:
- state count: state odd: state even: state size: id: name: salary: birthday: birthday格式化:
有条件的展示数据:
获取session作用域中数据:
效果
1.3 Themeleaf中引入静态资源静态资源默认放在static包里面
默认静态资源从static里面找,所以找静态资源的时候直接 项目名/静态资源名 就ok了,
themeleaf模板中引入静态资源
注意: @{/}代表通过thymeleaf语法动态获取应用名
在js代码中获取项目名
注意:[[书写thymeleaf语法]],这里[[]]是thymeleaf内嵌表达式



