SpringBoot的视图技术:
Springboot 为了简化项目开发 整合了一些常用视图 可以完成页面动态化
SpringBoot中的可整合的模板引擎技术:
Thymeleaf
freeMarker
groory
…
Thymeleaf基本用法;
一种基于服务器端的java模板引擎技术
xml xhtml html html5
其中包含丰富标签 函数 表达式
一般情况下 使用Springboot 开发项目时 都会使用 Thymeleaf模板引擎
Thymeleaf的常用标签:
引入Thymeleaf模板
//使用th标签是 Thymeleaf提供的标签 表示为模板 引入外联样式 //动态显示文本标签内容你好欢迎
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210718165448657.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0h1YW5UaWFuMTk5OQ==,size_16,color_FFFFFF,t_70#pic_center) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210718165448653.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0h1YW5UaWFuMTk5OQ==,size_16,color_FFFFFF,t_70#pic_center)注:
上述所有标记都是Html规范下的
h5的规范有所不同
不是使用 th:* 而是使用data-th-*
引入Thymeleaf模板
你好欢迎
标准表达式变量表达式: ${}
获取上下文中的变量的值
我是个小标题啊
在上下文中寻找title变量 如果有使用变量值替换 我是个小标题啊 如果没有显示 我是个小标题啊 Thymeleaf中的内置对象: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210718165505994.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0h1YW5UaWFuMTk5OQ==,size_16,color_FFFFFF,t_70#pic_center)选择变量表达式:*{}
用法和变量表达式相似 类似于xx里的yy
//上下文中的fristname变量
消息表达式:#{} 主要用于国际化内容 链接url表达式:@{} 用于跳转和查找 片段表达式:~{} 引入片段到模板中
name: xx //user中的lastname变量
surnname: xx //user中age变量
age:xx
Thymeleaf的基本使用:
- Thymeleaf模板基本配置
在Springboot 项目中使用模板必须引入依赖
org.springframework.boot
spring-boot-starter-thymeleaf
2.配置Thymeleaf的配置参数 在全局配置文件中编写
spring.thymeleaf.cache=true
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=claspath:/templates/
spring.thymeleaf.suffix=.html - 静态资源的访问
从 public resources static 这个顺序去寻找
使用Thymeleaf完成数据的页面展示
步骤:
-
创建项目 引入依赖
-
编写配置文件 对Thymeleaf数据进行设置
-
编写控制类
4. 引入静态资源
- 测试