1、创建springboot项目,导入thymeleaf web开发包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、application.properties添加thymeleaf配置项
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
示例源码:https://gitee.com/lion123/springboot-thymeleaf-demo
thymeleaf常用语法
在html页面中引入thymeleaf命名空间
变量表达式(获取变量值)
如果有数据,被替换完成前后端分离效果
字符截取长度
Login
th:unless 于 th:if 恰好相反,只有表达式中的条件不成立,才会显示其内容。
下拉选择
@{/usethymeleaf}是Context相关的相对路径,在渲染时会自动添加上当前Web应用的Context名字,假设context名字为seconddemo,
那么结果应该是/seconddemo/usethymeleaf,即URL中以”/“开头的路径(比如/usethymeleaf将会加上服务器地址和域名和应用cotextpath,形成完整的URL。
绝对路径
相对路径
Content路径,默认访问static下的css文件夹
女
未知
引入模块
© 2020 xxx
Name: zhangsan.
Age: 18.
friend: Rose.
日期格式化
js取值
css取值