springBoot加入thymeleaf模板的方式

当我们使用 Spring Boot 开发 Web 应用程序时,通常需要使用一些模板引擎来动态渲染我们的 HTML 页面。其中,Thymeleaf 是一个非常流行的模板引擎,它与 Spring Boot 集成非常容易。接下来,我将提供一份完整的攻略,指导您如何将 Thymeleaf 模板引擎添加到 Spring Boot 中。

步骤1:添加依赖

首先,您需要配置 pom.xml 文件:

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

这个依赖项包含了 Thymeleaf 模板引擎及其依赖项。

步骤2:配置视图解析器(View Resolver)

这里,我们需要添加一个视图解析器(View Resolver),它将负责将 Thymeleaf 模板映射到控制器方法返回的视图。下面是一个示例配置:

@Configuration
public class WebConfig implements WebMvcConfigurer {

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

   @Bean
   public ISpringTemplateEngine thymeleafTemplateEngine() {
       SpringTemplateEngine engine = new SpringTemplateEngine();
       engine.setEnableSpringELCompiler(true);
       engine.setTemplateResolver(thymeleafTemplateResolver());
       return engine;
   }

   @Bean
   public ITemplateResolver thymeleafTemplateResolver() {
       final SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
       resolver.setPrefix("classpath:/templates/");
       resolver.setTemplateMode(TemplateMode.HTML);
       resolver.setSuffix(".html");
       resolver.setTemplateMode("HTML5");
       resolver.setCharacterEncoding("UTF-8");
       resolver.setCacheable(false);
       return resolver;
   }
}

这个配置文件将以“classpath:/templates/”为目录加载 HTML 模板,并将 HTML5 视为默认模板模式。

步骤3:创建 Thymeleaf 模板

现在,您可以创建一个 Thymeleaf 模板了。

例如,创建一个包含 Thymeleaf 变量的 header.html 文件:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<header th:fragment="header">
    <nav>
        <ul>
            <li><a th:href="@{/}">首页</a></li>
            <li><a th:href="@{/about}">关于我们</a></li>
            <li><a th:href="@{/contact}">联系我们</a></li>
        </ul>
    </nav>
</header>
</body>
</html>

注意,在模板中,使用 th: 前缀声明变量。

步骤4:创建控制器并返回 Thymeleaf 视图

接下来,您需要创建一个控制器,它将返回您刚才创建的 Thymeleaf 模板。

例如,创建一个 HomeController 类:

@Controller
public class HomeController {

   @GetMapping("/")
   public String home(Model model) {
       return "home";
   }

   @GetMapping("/about")
   public String about(Model model) {
       return "about";
   }

   @GetMapping("/contact")
   public String contact(Model model) {
       return "contact";
   }
}

这个控制器有 3 个处理方法,分别对应于上面 header.html 中声明的 3 个链接。您可以在这些处理方法中使用 Model 对象来设置变量。

例如,创建一个 home.html 文件来显示 home 控制器的内容:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <header th:replace="header :: header"></header>
    <div>
        <h2>欢迎来到我的博客!</h2>
        <p>这是一个 Spring Boot + Thymeleaf 的示例项目。</p>
    </div>
</body>
</html>

如此简单,我们已经完成了将 Thymeleaf 模板引擎添加到 Spring Boot 项目中的过程。

示例1:添加变量到模板中

您也可以使用 Model 对象来在模板中添加变量。例如,您可以添加一个时间戳变量:

@GetMapping("/time")
public String time(Model model) {
   model.addAttribute("time", LocalDateTime.now());
   return "time";
}

然后在模板 time.html 中使用它:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <title>时间戳</title>
</head>
<body>
   <header th:replace="header :: header"></header>
   <div>
       <h2>现在的时间是:</h2>
       <p th:text="${time}">2022-07-30T21:34:35.902</p>
   </div>
</body>
</html>

示例2:循环添加变量

对于更复杂的模板,您可以使用 Thymeleaf 的循环功能来遍历集合并动态添加变量。例如,如果有一个包含简单文章对象的列表:

@GetMapping("/articles")
public String articles(Model model) {
    List<Article> articles = Arrays.asList(
            new Article("Spring Boot 入门教程", "2022-07-30"),
            new Article("Spring Boot 自动配置原理", "2022-07-31"),
            new Article("Spring Boot + Thymeleaf 实战", "2022-08-01")
    );
    model.addAttribute("articles", articles);
    return "articles";
}

则可以在 articles.html 模板中使用以下代码将其呈现:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文章列表</title>
</head>
<body>
   <header th:replace="header :: header"></header>
   <h2>文章列表</h2>
    <table>
        <tr>
            <th>标题</th>
            <th>日期</th>
        </tr>
        <tr th:each="article : ${articles}">
            <td th:text="${article.title}">Spring Boot 入门教程</td>
            <td th:text="${article.date}">2022-07-30</td>
        </tr>
    </table>
</body>
</html>

这是将 Thymeleaf 模板引擎添加到 Spring Boot 项目中的简单过程,同时也展示了一些模板的常见用法。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springBoot加入thymeleaf模板的方式 - Python技术站

(0)
上一篇 2023年5月30日
下一篇 2023年5月30日

相关文章

  • Win10系统安装字体后Edge浏览器打开网页出现乱码的原因及解决方法

    下面是关于“Win10系统安装字体后Edge浏览器打开网页出现乱码的原因及解决方法”的完整攻略。 问题背景 在使用Edge浏览器浏览网页时,有时会出现网页中文乱码的问题,其原因是安装了新的字体后导致Edge浏览器无法正确显示网页文字。 问题原因及解决方法 字体缺失或损坏 如果浏览器无法找到所需的字体,就会出现网页显示乱码的错误。此时,我们需要检查系统中是否缺…

    html 2023年5月31日
    00
  • springboot页面国际化配置指南

    下面我将详细讲解“Spring Boot 页面国际化配置指南”的完整攻略。 前言 在当前的全球化时代,应用程序需要支持多种语言和文化,因此国际化已成为开发项目的一个重要特性。Spring Boot 提供了一种非常方便的方式来实现页面国际化,本文将介绍如何在 Spring Boot 中配置页面国际化,帮助开发者更好地支持不同语言和文化环境。 步骤 1. 创建资…

    html 2023年5月30日
    00
  • php+xml实现在线英文词典查询的方法

    PHP+XML实现在线英文词典查询的方法可以通过以下步骤实现: 步骤1:创建XML文件 首先,我们需要创建一个XML文件来存储英文单词和对应的解释。可以使用任何文本编辑器来创建XML文件,以下是一个示例: <dictionary> <word> <term>apple</term> <definition…

    html 2023年5月30日
    00
  • VLC播放器播放电影字幕出现方块乱码怎么办?

    首先,我们需要了解字幕文件的编码方式和VLC播放器的设置。 VLC播放器默认情况下会自动检测字幕文件的编码方式,但是有些字幕文件的编码方式并不常见,或者是由于某些原因导致编码出现错误时,就有可能出现方块乱码的情况。为了解决这个问题,我们可以手动设置VLC播放器的字幕编码方式。 以下是具体步骤: 1.打开VLC播放器,单击左上角的“工具”菜单,然后选择“首选项…

    html 2023年5月31日
    00
  • jsp和servlet操作mysql中文乱码问题的解决办法

    解决jsp和servlet操作mysql中文乱码问题可以分为以下几个步骤:1. 创建数据库和表时,设置编码为utf8mb4,保证数据库和表的编码一致2. 在jsp页面中,设置编码为utf-83. 在servlet中,设置请求编码和响应编码为utf-84. 在连接数据库时,设置连接编码为utf8mb45. 在执行sql语句时,设置编码为utf8mb4 具体实现…

    html 2023年5月31日
    00
  • Spring AOP 切面@Around注解的用法说明

    Spring AOP是面向切面编程的一种实现,主要用于处理横切关注点(Cross-Cutting Concerns)的问题,它可以支持在程序执行的不同阶段插入增强的逻辑,从而实现对代码进行统一的管理。在Spring AOP中,我们可以通过@Around注解定义切面,本文将针对@Around注解的使用详细说明。 1. @Around注解的基本用法 @Aroun…

    html 2023年5月30日
    00
  • xmlhttp 乱码 比较完整的解决方法 (UTF8,GB2312 编码 解码)

    下面是关于“xmlhttp 乱码 比较完整的解决方法”的攻略。 问题描述 在使用XMLHttpRequest对象进行请求时,有可能会出现中文乱码的问题,这是因为我们在使用XMLHttpRequest对象时,需要设置编码方式。 解决方法 1. 设置发送请求时的编码 设置XMLHttpRequest对象的charset属性为”UTF-8″,即可保证中文数据传输不…

    html 2023年5月30日
    00
  • winPE系统启动时出现乱码(编码是ANSI)怎么办

    针对“winPE系统启动时出现乱码(编码是ANSI)怎么办”的问题,我将提供以下攻略: 问题描述 当使用winPE系统启动时,可能会出现乱码的情况,此时文本编码格式可能是ANSI,导致文本无法正确显示。 解决方法 方法1: 替换乱码字体文件 下载适用于winPE系统的字体文件,比如微软雅黑等中文字体。 将下载的字体文件(xxxx.ttf)拷贝到winPE系统…

    html 2023年5月31日
    00
合作推广
合作推广
分享本页
返回顶部