栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

thymeleaf语法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

thymeleaf语法

thymeleaf是基于HTML的

1. 坐标

	org.springframework.boot
	spring-boot-starter-thymeleaf

2. 在文件夹templates下创建html文件

在文件的html根标签上添加命名空间




    
    Title


    


3. 基础语法
th:text = ""  文本输出 : 可替换标签间的文本内容
th:value = "" 只能用在intput元素
  • $ {x}将返回存储在Thymeleaf上下⽂中的变量x或作为请求属性。
  • $ {param.x}将返回⼀个名为x的请求参数(可能是多值的)。
  • $ {session.x}将返回⼀个名为x的会话属性。
  • $ {application.x}将返回⼀个名为x的servlet上下⽂属性。
3.1 内置对象strings

使用内置对象用#引用
大多数内置对象都以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小写
3.2 内置对象dates


    
    FOREACH


    

3.2 条件判断
th:if

在controller的方法中

	model.addAttribute("sex","男");

在html中

	
性别 : 男 性别 : 女
th:switch / th:case

注意

表示java中switch中的default,即没有case值为true时,显示的内容

在controller的方法中

	model.addAttribute("id","1");
	// model.addAttribute("id",1); 二者都行 , thymeleaf最终都会转为字符串类型

在html中

	
ID : 1 ID : 2 ID : 3 ID : *
3.3 迭代遍历

th:each

迭代器,用于循环迭代集合

创建User实体类

	@Data
	public class User {
      public int id;
      public String userName;
      public String job;
	}

在controller的方法中

	List list = new ArrayList<>();
	list.add(new User(... , ... , ... ));// 添加多个User对象
	list.add(new User(... , ... , ... ));
	list.add(new User(... , ... , ... ));
	model.addAttribute("list",list);

在html中

    			// 迭代对象 , 迭代器 : 集合
             // int 
             // int 
              // int
               // boolean 奇数行为true
              // boolean 偶数行为true
             // boolean 第一行为true
              // boolean 最后一行为true
        
id 用户名 职业 index count size odd even first last

注意

迭代遍历 List与Set 是一致的
3.4 迭代Map
th:each

在controller的方法中

	Map map = new HashMap<>();
	map.put("user1",new User(... , ... , ... ));// 添加多个User对象
	map.put("user2",new User(... , ... , ... ));
	map.put("user3",new User(... , ... , ... ));
	model.addAttribute("map",map);

在html中

    					// 迭代对象  : 集合
            		// 想获取键名 , 使用 th:text="${u.key}"
            
id 用户名 职业 键名
3.5 操作域对象
HttpServletRequest 、HttpSession 、 ApplicationContext

在controller的方法中

	request.setAttribute("req","HttpServletRequest");
	request.getSession().setAttribute("sess","HttpSession");
	request.getSession().getServletContext().setAttribute("application","ApplicationContext");

在html中

 

HttpServletRequest 获取方式一 :

HttpServletRequest 获取方式二 :

HttpSession 获取方式一 :

HttpSession 获取方式二 :

ApplicationServletContext 获取方式一 :

ApplicationServletContext 获取方式二 :

4. URL表达式
语法格式为@{ }
4.1 绝对路径
    
    相对于当前项目的根
    相对于tomcat服务器的根
4.2 相对路径
    相对于当前项目的根
    相对于tomcat服务器的根
  • 相对于tomcat服务器的根 :
    • 当前项目的目录 的上一级目录
4.3 在URL中传递参数
	@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=123
5. 配置文件
默认
spring:
	thymeleaf:
		prefix: classpath:/templates/
		suffix: .html
		model: HTML # 默认为HTML4, 可修改为HTML5
动态页面放在templates目录的子目录下
spring:
	thymeleaf:
		prefix: classpath:/templates/子目录/
		suffix: .html
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/695531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号