(四)使用 Thymeleaf 模板引擎 —— 《一步一步学 Spring Boot 2》读书笔记

本文纯个人读书笔记,书籍《一步一步学 Spring Boot 2》
如果喜欢,可直接购买书籍。如有侵权,请联系删除

一、Thymeleaf 简介

Thymeleaf 是一个优秀的面向 Java 的 XML/XHTML/HTML5 页面模板,并具有丰富的标签语言和函数。因此,在使用 Spring Boot 框架进行页面设计,一般都会选择 Thymeleaf 模板。

常用表达式:

${...}  变量表达式
*{...}  选择表达式
#{...}  消息文字表达式
@{...}  链接url表达式
#maps   工具对象表达式

常用标签:

th:action  定义后台控制器路径
th:each  循环语句
th:field  表单字段绑定
th:href 定义超链接
th:id  div标签中的id声明,类似html标签中的id属性
th:if  条件判断语句
th:include  布局标签,替换内容到引入文件
th:fragment  布局标签,定义一个代码片段,方便其它地方引用
th:object  替换对象
th:src  图片类地址引入
th:text  显示文本
th:value  属性赋值

常用函数:

#dates  日期函数
#lists  列表函数
#arrays  数组函数
#strings  字符串函数
#numbers  数字函数
#calendars  日历函数
#objects  对象函数
#bools  逻辑函数

附: 官网链接。

二、Thymeleaf 的使用

1.引入依赖

在 pom 文件中添加依赖。

pom.xml:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.添加配置

在 application.properties 文件中添加 Thymelea f配置。

application.properties:

#thymeleaf配置
#模板的模式,支持如:HTML、XML、TEXT、JAVASCRIPT等
spring.thymeleaf.mode=HTML5
#编码,可不用配置
spring.thymeleaf.encoding=UTF-8
#内容类别,可不用配置
spring.thymeleaf.content-type=text/html
#开发配置为false,避免修改模板还要重启服务器
spring.thymeleaf.cache=false
#配置模板路径,默认就是templates,可不用配置
#spring.thymeleaf.prefix=classpath:/templates/

注: Thymeleaf 模板引擎默认会读取 my-spring-boot 项目下的资源文件夹 resource 下的 templates 目录,这个目录是用来存放 HTML 文件的。如果我们添加了 Thymeleaf 依赖,而没有进行任何配置,或者添加默认目录,启动应用时就会报错。

3.控制层

新建控制类 com.xiaoyue.demo.controller.UserController.java 。

UserController.java :

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);

        return "user";
    }
}

4.页面

使用 Thymeleaf 在 /src/main/resources/templates 目录下进行 user.html 页面的开发。

user.html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table>
    <tr>
        <td>用户名</td>
        <td>密码</td>
    </tr>
    <tr th:each="user:${users}">
        <td th:text="${user.name}"></td>
        <td th:text="${user.password}"></td>
    </tr>
</table>
</body>
</html>

http://www.thymeleaf.org 是 Thymeleaf 命名空间,通过引入该命名空间,就可以在 HTML 文件中使用 Thymeleaf 标签语言,用关键字 “th” 来标注。

//th:text用于显示文本Hello,Thymeleaf
<p th:text=" 'Hello,Thymeleaf' "></p>
//${} 关键字用于获取内存变量为name的值 
<p th:text="${name}"></p>
//th:src用于设定![在这里插入图片描述]()图片文件的链接地址  @{} 超链接url表达式
![在这里插入图片描述]()

5.运行

运行 DemoApplication.java 中的 main 函数,访问地址 http://localhost:8080/user/list 即可。
在这里插入图片描述
如果在 application.properties 中配置了 server.servlet.context-path=/demo,则需要在访问地址中同样加上 context-path。访问地址为 http://localhost:8080/demo/user/list

三、Rest Client

Rest Clien t是一个用于测试 RESTful Web Service 的 Java 客户端。非常小巧,界面非常简单。Intellij IDEA 软件已经集成该插件,方便我们进行调试。我们可以在 Tools 里面进行打开。

在这里插入图片描述

使用 Rest Client

在 Rest Client 窗口中选择请求方式 HTTP method,输入主机t和端口 Host/port,以及填入访问的映射路径 Path,最后点击右上角的run按钮,就可以进行访问。
在这里插入图片描述
同时可以在 Request、Cookies、Response、Response Headers 查看请求信息、Cookies 信息、响应信息、请求头信息等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值