Spring Boot 中的模板引擎是什么,如何使用

Spring Boot 中的模板引擎是什么,如何使用

在 Web 应用程序中,模板引擎是一种用于动态生成 HTML、XML、JSON 等文档的工具。Spring Boot 内置了多种常见的模板引擎,例如 Thymeleaf、Freemarker、Velocity 等,让我们可以轻松地创建动态网页。

本文将介绍 Spring Boot 中常用的模板引擎 Thymeleaf 的使用方法,包括如何配置、如何使用模板、如何传递数据等。

在这里插入图片描述

Thymeleaf 的配置

在 Spring Boot 中使用 Thymeleaf 非常简单,只需要在项目的依赖中添加以下两个依赖:

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

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>webjars-locator-core</artifactId>
</dependency>

上面的第一个依赖是 Spring Boot 的 Thymeleaf 启动器,它包含了 Thymeleaf 的必需依赖,并自动配置了 Thymeleaf 的视图解析器。第二个依赖是 WebJars,它是一个用于管理 Web 前端库的工具,我们可以使用它来引入 Thymeleaf 的 HTML 标签库。

在添加了依赖之后,我们还需要在配置文件中添加以下配置:

# 开启 Thymeleaf 缓存
spring.thymeleaf.cache=true

# Thymeleaf 模板所在的目录,默认为 classpath:/templates/
spring.thymeleaf.prefix=classpath:/templates/

# Thymeleaf 模板文件的后缀,默认为 .html
spring.thymeleaf.suffix=.html

# Thymeleaf 模板文件的编码,默认为 UTF-8
spring.thymeleaf.encoding=UTF-8

# 启用 Thymeleaf 的 HTML 标签库
spring.thymeleaf.mode=HTML

以上配置中,spring.thymeleaf.cache 属性用于开启 Thymeleaf 的缓存,以提高性能。spring.thymeleaf.prefix 属性用于指定 Thymeleaf 模板所在的目录,默认为 classpath:/templates/spring.thymeleaf.suffix 属性用于指定 Thymeleaf 模板文件的后缀,默认为 .htmlspring.thymeleaf.encoding 属性用于指定 Thymeleaf 模板文件的编码,默认为 UTF-8。spring.thymeleaf.mode 属性用于启用 Thymeleaf 的 HTML 标签库。

在完成了上述配置之后,我们就可以开始使用 Thymeleaf 了。

Thymeleaf 的使用

创建模板文件

首先,我们需要在 src/main/resources/templates 目录下创建一个 Thymeleaf 模板文件。例如,我们创建一个名为 index.html 的模板文件,文件内容如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf Demo</title>
</head>
<body>
    <h1 th:text="${message}">Hello World!</h1>
</body>
</html>

在上面的模板文件中,我们使用了 Thymeleaf 的 th:text 属性来动态显示消息。${message} 表示从控制器传递过来的数据,它将会被替换为实际的消息内容。

创建控制器

接下来,我们需要创建一个控制器来处理请求,并将数据传递给模板。例如,我们创建一个名为 DemoController 的控制器,代码如下:

@Controller
public class DemoController {
    
    
    @GetMapping("/")
    public String index(Model model) {
    
    
        model.addAttribute("message", "Hello Thymeleaf!");
        return "index";
    }
}

在上面的控制器中,我们使用了 @Controller 注解来定义一个控制器,它包含了一个 index 方法,用于处理根路径的 GET 请求。在 index 方法中,我们使用了 Model 对象来传递数据给模板,将一个名为 message 的属性设置为 Hello Thymeleaf!

运行程序

最后,我们需要启动程序并访问根路径,以查看 Thymeleaf 的效果。可以使用 Spring Boot 提供的内置 Web 服务器来运行程序,例如使用以下命令:

mvn spring-boot:run

启动成功后,在浏览器中访问 http://localhost:8080/,将会看到以下内容:

Hello Thymeleaf!

这证明 Thymeleaf 已经成功地将模板和数据结合起来,动态生成了 HTML 页面。

Thymeleaf 的语法

Thymeleaf 的语法非常简单,它提供了类似于 JSP 的标签和表达式。以下是一些常用的 Thymeleaf 语法:

变量表达式

使用 ${} 语法来表示变量表达式,例如:

<h1 th:text="${message}">Hello World!</h1>

在上面的例子中,${message} 表示从控制器传递过来的数据。

选择表达式

使用 *{} 语法来表示选择表达式,例如:

<input type="text" th:value="*{user.name}" />

在上面的例子中,*{user.name} 表示一个名为 user 的对象的 name 属性。

URL 表达式

使用 @{} 语法来表示 URL 表达式,例如:

<a th:href="@{/users/{id}(id=${user.id})}">Edit</a>

在上面的例子中,@{/users/{id}(id=${user.id})} 表示一个动态 URL,其中 {id} 表示一个参数,${user.id} 表示参数的值。

条件判断

使用 th:ifth:unless 来进行条件判断,例如:

<div th:if="${user.isAdmin}">
    <p>Welcome, administrator!</p>
</div>
<div th:unless="${user.isAdmin}">
    <p>Welcome, user!</p>
</div>

在上面的例子中,th:if 表示当表达式为真时显示,th:unless 表示当表达式为假时显示。

循环遍历

使用 th:each 来进行循环遍历,例如:

<ul>
    <li th:each="user : ${users}" th:text="${user.name}"></li>
</ul>

在上面的例子中,th:each 表示对 ${users} 集合进行遍历,将每个元素赋值给 user 变量,然后显示 user.name 属性。

更多的 Thymeleaf 语法可以参考官方文档:https://www.thymeleaf.org/documentation.html

总结

本文介绍了 Spring Boot 中常用的模板引擎 Thymeleaf 的使用方法,包括如何配置、如何使用模板、如何传递数据等。Thymeleaf 是一款功能强大、易于上手的模板引擎,它提供了简单的语法和丰富的标签库,让我们可以轻松地创建动态网页。

猜你喜欢

转载自blog.csdn.net/2302_77835532/article/details/131577214