SpringBoot视图技术(2)

82 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第20天,点击查看活动详情

Thymeleaf完成页面的数据显示

下面完成一个简单的工程

搭建的步骤:

• 创建Spring Boot项目,引入Thymeleaf依赖;

• 编写配置文件,对Thymeleaf模板的页面数据缓存进行设置;

• 创建Web控制类:@Controller

• 创建模板页面并引入静态资源文件;

• 效果测试。

1.创建Spring Boot项目,引入必备依赖:Web、Thymeleaf

image.png

2.ThymeleafProperties类:Thymeleaf 的所有属性。

文件(后缀 .html)存放在/templates/目录中。

image.png

3.编写配置文件,设置页面数据缓存

image.png

4.在controller包下,编写一个TestController控制器类

image.png

5.编写一个测试页面 test.html 放在 templates 目录下

image.png

6.运行成功

输入网址:http://localhost:8080/test

image.png

7.下面在test.html基础上,添加thymeleaf标签,实现动态数据替换功能。

8.修改TestController类

image.png

9.修改test.html

image.png

其中需要注意的是:

• th:text:用于指定标签显示的文本内容

• ${...}:变量表达式,获取上下文中的变量值

10.运行测试

image.png

11.迭代标签<th:each>

image.png

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第20天,点击查看活动详情