thymeleaf是基于HTML的
1. 坐标2. 在文件夹templates下创建html文件org.springframework.boot spring-boot-starter-thymeleaf
在文件的html根标签上添加命名空间
Title
3. 基础语法
th:text = "" 文本输出 : 可替换标签间的文本内容 th:value = "" 只能用在intput元素
- $ {x}将返回存储在Thymeleaf上下⽂中的变量x或作为请求属性。
- $ {param.x}将返回⼀个名为x的请求参数(可能是多值的)。
- $ {session.x}将返回⼀个名为x的会话属性。
- $ {application.x}将返回⼀个名为x的servlet上下⽂属性。
使用内置对象用#引用
大多数内置对象都以s结尾
| 方法 | 含义 |
|---|---|
| ${#strings.isEmpty()} | 判断是否为空,空为true |
| ${#strings.contains(msg,‘T’)} | 判断msg中是否包含字符(串)T |
| ${#strings.startsWith(msg,子串)} | 判断msg中是否以字符(串)开头 |
| ${#strings.endsWith(msg,字串)} | 判断msg中是否以字符(串)结尾 |
| ${#strings.length(字符串)} | 返回字符串长度 |
| ${#strings.indexOf(msg,字串)} | 查找子串的位置,返回该字串下标,没有返回-1 |
| ${#strings.substring(msg,2)} | 截取msg从2到结束 |
| ${#strings.substring(msg,2,5)} | 截取msg从2到5 |
| ${#strings.toUpperCase(msg)} | msg大写 |
| ${#strings.toLowerCase(msg)} | msg小写 |
FOREACH
th:if
在controller的方法中
model.addAttribute("sex","男");
在html中
性别 : 男 性别 : 女
th:switch / th:case
注意
| 在 |
在controller的方法中
model.addAttribute("id","1");
// model.addAttribute("id",1); 二者都行 , thymeleaf最终都会转为字符串类型
在html中
3.3 迭代遍历ID : 1 ID : 2 ID : 3 ID : *
th:each
迭代器,用于循环迭代集合
创建User实体类
@Data
public class User {
public int id;
public String userName;
public String job;
}
在controller的方法中
Listlist = new ArrayList<>(); list.add(new User(... , ... , ... ));// 添加多个User对象 list.add(new User(... , ... , ... )); list.add(new User(... , ... , ... )); model.addAttribute("list",list);
在html中
| id | 用户名 | 职业 | index | count | size | odd | even | first | last |
|---|---|---|---|---|---|---|---|---|---|
| // int | // int | // int | // boolean 奇数行为true | // boolean 偶数行为true | // boolean 第一行为true | // boolean 最后一行为true |
注意
| 迭代遍历 List与Set 是一致的 |
th:each
在controller的方法中
Mapmap = new HashMap<>(); map.put("user1",new User(... , ... , ... ));// 添加多个User对象 map.put("user2",new User(... , ... , ... )); map.put("user3",new User(... , ... , ... )); model.addAttribute("map",map);
在html中
| id | 用户名 | 职业 | 键名 |
|---|---|---|---|
| // 想获取键名 , 使用 th:text="${u.key}" |
HttpServletRequest 、HttpSession 、 ApplicationContext
在controller的方法中
request.setAttribute("req","HttpServletRequest");
request.getSession().setAttribute("sess","HttpSession");
request.getSession().getServletContext().setAttribute("application","ApplicationContext");
在html中
4. URL表达式HttpServletRequest 获取方式一 :
HttpServletRequest 获取方式二 :
HttpSession 获取方式一 :
HttpSession 获取方式二 :
ApplicationServletContext 获取方式一 :
ApplicationServletContext 获取方式二 :
| 语法格式为@{ } |
相对于当前项目的根 相对于tomcat服务器的根4.2 相对路径
相对于当前项目的根 相对于tomcat服务器的根
- 相对于tomcat服务器的根 :
- 当前项目的目录 的上一级目录
@GetMapping("/show")
public String show(String username,String password){
// ...
return "";
}
4.3.1 普通风格
4.3.2 Restful风格
方式一```java @GetMapping("/show/{username}") public String show(@PathVariable String username){ // ... return ""; } ```
http://127.0.0.1:8080/show/zhangsan
方式二
@GetMapping("/show/{username}/{password}")
public String show(@PathVariable String username,@PathVariable String password){
// ...
return "";
}
http://127.0.0.1:8080/show/zhangsan/123
方式三
@GetMapping("/show/{username}")
public String show(@PathVariable String username,String password){
// ...
return "";
}
http://127.0.0.1:8080/show/zhangsan?password=123
方式四
@GetMapping("/show/{username}")
public String show(@PathVariable String username,String password){
// ...
return "";
}
http://127.0.0.1:8080/show/zhangsan?password=1235. 配置文件
默认
spring: thymeleaf: prefix: classpath:/templates/ suffix: .html model: HTML # 默认为HTML4, 可修改为HTML5
动态页面放在templates目录的子目录下
spring: thymeleaf: prefix: classpath:/templates/子目录/ suffix: .html



