SpringMVC 中的视图如何使用 Thymeleaf

SpringMVC 中的视图如何使用 Thymeleaf

SpringMVC 是一个基于 Java 的 Web 框架,它是 Spring 框架的一部分,提供了一系列的组件和工具,帮助开发人员构建 Web 应用程序。其中,视图是 Spring MVC 中的核心组件之一,它负责将模型数据渲染成 HTML 页面并返回给客户端。Thymeleaf 是一种流行的模板引擎,它可以方便地将模型数据渲染成 HTML 页面。本文将介绍 SpringMVC 中的视图如何使用 Thymeleaf,并提供示例代码。

在这里插入图片描述

Thymeleaf 简介

Thymeleaf 是一个流行的模板引擎,它可以方便地将模型数据渲染成 HTML 页面。Thymeleaf 的模板语法与 HTML 语法非常相似,可以直接在 HTML 页面中嵌入 Thymeleaf 表达式。Thymeleaf 还提供了一系列的标准标签和属性,可以对 HTML 页面进行动态渲染。

下面是一个简单的 Thymeleaf 表达式示例:

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

在上面的代码中,我们使用了 Thymeleaf 的 th:text 属性来渲染模型数据。这个属性会将 ${message} 表达式替换为模型中的 message 变量。

Thymeleaf 集成到 SpringMVC 中

Thymeleaf 可以与 SpringMVC 集成,用于渲染视图。下面我们来看一下如何将 Thymeleaf 集成到 SpringMVC 中。

步骤一:添加 Thymeleaf 依赖

在 pom.xml 文件中添加 Thymeleaf 的依赖:

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

步骤二:配置 Thymeleaf 模板解析器

在 SpringMVC 的配置文件中,配置 Thymeleaf 模板解析器:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
}

在上面的代码中,我们创建了一个 SpringResourceTemplateResolver 对象,指定了 Thymeleaf 模板文件的位置和后缀名。然后,我们创建了一个 SpringTemplateEngine 对象,并将模板解析器设置为其属性。最后,我们创建了一个 ThymeleafViewResolver 对象,并将模板引擎设置为其属性。

步骤三:使用 Thymeleaf 视图

在控制器中,我们可以使用 Thymeleaf 视图来渲染模型数据。例如:

@Controller
public class HelloWorldController {

    @GetMapping("/hello")
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello, World!");
        return "hello";
    }
}

在上面的代码中,我们使用了 Model 对象来添加模型数据,并将 Thymeleaf 视图名称设置为 “hello”。Thymeleaf 视图名称的格式为 “文件名:后缀”,其中文件名是在 Thymeleaf 模板文件夹中的相对路径,后缀是 Thymeleaf 模板文件的后缀名。

步骤四:创建 Thymeleaf 模板文件

最后,我们需要在 Thymeleaf 模板文件夹中创建一个对应的模板文件。例如,在上面的示例中,我们需要在 “/WEB-INF/views/” 目录下创建一个名为 “hello.html” 的文件,文件内容如下:

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

在上面的代码中,我们使用了 Thymeleaf 的 th:text 属性来渲染模型数据。这个属性会将 ${message} 表达式替换为模型中的 message 变量。

完整示例代码

下面是一个完整的使用 Thymeleaf 的 SpringMVC 示例代码:

WebConfig.java

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
}

HelloWorldController.java

@Controller
public class HelloWorldController {

    @GetMapping("/hello")
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello, World!");
        return "hello";
    }
}

hello.html

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

总结

本文介绍了如何在 SpringMVC 中使用 Thymeleaf 来渲染视图。我们首先简要介绍了 Thymeleaf 的基本语法和特点,然后详细介绍了如何将 Thymeleaf 集成到 SpringMVC 中,并提供了示例代码。使用 Thymeleaf 可以方便地将模型数据渲染成 HTML 页面,并且 Thymeleaf 的语法与 HTML 的语法非常相似,易于上手。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员徐师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值