Spring Boot入门(十三) 之 thymeleaf一些常用语法规则

1.th:text

用于改变当前元素里边的文本内容,里边的内容会被转义的
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
这样我们后台的name表示的值就可以传到这个div里边,页面显示如下
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
当然如果div元素里边本身就有文字,那么th:textl里边的内容就会覆盖div里边本身的内容
我们刚刚说的这个是转义过的,如下例子
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
被转义后,里边是什么就是什么,有标签也不给你解析,与之相对应的是`th:utext

2.th:utext

Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
这样就会被解析成h1大标题

3.th:idth:class

Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
这样可以动态指定他的id和class
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则

4.th:each

each标签可以取出里边的所有内容并遍历,用法如下:
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
这里边的th:each表示遍历user里边的内容,用u取出每一个内容,然后放入h4标签里边,并且,user里边有多少元素,h4就会有多少标签,结果如下:
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
三个h4标签来遍历结果
看下边的例子,我们也可以使用块内元素来表示
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
注意这里的[[ ]]里边的内容是转义过得,里边的标签就转义成了普通字符串
[( )]里边的内容则是不转义的,标签会被解析
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则