Spring Boot学习记录–thymeleaf模板
来源:互联网 发布:网络编辑培训课程 编辑:程序博客网 时间:2024/06/05 16:55
自从来公司后都没用过jsp当界面渲染了,因为前后端分离不是很好,反而模板引擎用的比较多,thymeleaf最大的优势后缀为html,就是只需要浏览器就可以展现页面了,还有就是thymeleaf可以很好的和spring集成.下面开始学习.
1.引入依赖
maven中直接引入
- 1
- 2
- 3
- 4
可以查看依赖关系,发现spring-boot-starter-thymeleaf下面已经包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依赖去掉.
2.配置视图解析器
spring-boot很多配置都有默认配置,比如默认页面映射路径为
classpath:/templates/*.html
同样静态文件路径为
classpath:/static/
在application.properties中可以配置thymeleaf模板解析器属性.就像使用springMVC的JSP解析器配置一样
- 1
- 2
- 3
- 4
- 5
- 6
- 7
具体可以配置的参数可以查看 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
这个类,上面的配置实际上就是注入到该类中的属性值.
3.编写DEMO
1.控制器
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2.view(注释为IDEA生成的索引,便于IDEA补全)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3.效果
4.基础语法
回味上面的DEMO,可以看出来首先要在改写html标签
- 1
- 2
这样的话才可以在其他标签里面使用th:*
这样的语法.这是下面语法的前提.
1.获取变量值
- 1
- 2
可以看出获取变量值用$
符号,对于javaBean的话使用变量名.属性名
方式获取,这点和EL表达式一样.
另外$
表达式只能写在th标签内部,不然不会生效,上面例子就是使用th:text
标签的值替换p
标签里面的值,至于p里面的原有的值只是为了给前端开发时做展示用的.这样的话很好的做到了前后端分离.
2.引入URL
Thymeleaf对于URL的处理是通过语法@{…}来处理的
- 1
- 2
- 3
类似的标签有:th:href
和th:src
3.字符串替换
很多时候可能我们只需要对一大段文字中的某一处地方进行替换,可以通过字符串拼接操作完成:
- 1
- 2
一种更简洁的方式是:
- 1
- 2
当然这种形式限制比较多,|…|中只能包含变量表达式${…},不能包含其他常量、条件表达式等。
4.运算符
在表达式中可以使用各类算术运算符,例如+, -, *, /, %
- 1
- 2
逻辑运算符>, <, <=,>=,==,!=都可以使用,唯一需要注意的是使用<,>时需要用它的HTML转义符:
- 1
- 2
5.条件
if/unless
Thymeleaf中使用th:if和th:unless属性进行条件判断,下面的例子中,标签只有在th:if中条件成立时才显示:
- 1
- 2
th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。
Switch
Thymeleaf同样支持多路选择Switch结构:
- 1
- 2
- 3
- 4
默认属性default可以用*表示:
- 1
- 2
- 3
- 4
- 5
6.循环
渲染列表数据是一种非常常见的场景,例如现在有n条记录需要渲染成一个表格
,该数据集合必须是可以遍历的,使用th:each标签:- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
可以看到,需要在被循环渲染的元素(这里是)中加入th:each标签,其中th:each=”prod : ${prods}”意味着对集合变量prods进行遍历,循环变量是prod在循环体中可以通过表达式访问。
7.Utilities
为了模板更加易用,Thymeleaf还提供了一系列Utility对象(内置于Context中),可以通过#直接访问:
- #dates
- #calendars
- #numbers
- #strings
- arrays
- lists
- sets
- maps
- …
下面用一段代码来举例一些常用的方法:
date
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
string
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
快速的学习还是直接写例子最快,后期写Demo遇到问题再加上去
参考链接: http://www.tianmaying.com/tutorial/using-thymeleaf
整合项目地址:
https://github.com/nl101531/JavaWEB
补充
在spring-boot1.4之后,支持thymeleaf3,可以更改版本号来进行修改支持.
3相比2极大的提高了效率,并且不需要标签闭合,类似的link,img等都有了很好的支持,按照如下配置即可
- Spring Boot学习记录–thymeleaf模板
- Spring Boot学习记录–thymeleaf模板
- Spring Boot学习记录(二)--thymeleaf模板
- Spring Boot学习记录(二)--thymeleaf模板
- Spring Boot学习记录(二)--thymeleaf模板
- Spring Boot学习–thymeleaf模板
- spring-boot thymeleaf模板学习
- Spring Boot–thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- Spring Boot使用thymeleaf模板
- spring-boot--整合thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- spring boot使用thymeleaf模板
- spring-boot--使用thymeleaf模板
- Spring Boot 使用 Thymeleaf模板
- spring-boot--使用thymeleaf模板
- json数据传输
- 欢迎使用CSDN-markdown编辑器
- qb模拟题
- oracle索引和数据库完整性
- Android Fragment的生命周期
- Spring Boot学习记录–thymeleaf模板
- linux下useradd命令详解
- 第二周 汉诺塔问题
- Oracle执行计划 讲解(一)
- How_to_Build_IGSTK(old)
- 重装jdk后遇到的一堆问题总结
- UICC之UICC框架
- KNN算法
- loadrunner Web_类函数之web_list()