6. 模板引擎 Thymeleaf

Thyme leaf 英译为 百里香的叶子。

模板引擎

​ 以前开发中使用的jsp就是一个模板引擎,但是springboot 以jar的方式,并且使用嵌入式的tomcat,所以默认不支持jsp

​ Springboot推荐使用模板引擎,除了jsp,还有用的比较多的freemarker,包括springboot推荐的Thymeleaf。它们的思想都是一样的,如下:

模板引擎的作用

​ 写一个页面模板,加上后台封装好的数据,交给模板引擎。它按照我们的数据进行表达式解析,填充到指定位置,最终生成想要的内容再写出去。

引入Thymeleaf

​ 怎么引入呢,对于springboot来说,只是一个start的事情。我们在项目中引入一下。

​ 提供三个网址:

Thymeleaf官网:https://www.thymeleaf.org/

Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf

Spring官方文档:https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#using-boot-starter

找到对应的pom依赖:

<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-java8time -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>

Maven会自动下载jar包,我们可以看到:

Thymeleaf分析

​ 我们首先按照springboot的自动配置原理看一下Thymeleaf的自动配置规则,找到自动配置类:ThymeleafProperties

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = DEFAULT_PREFIX;
private String suffix = DEFAULT_SUFFIX;
private String mode = "HTML";
private Charset encoding = DEFAULT_ENCODING;
}

​ 我们可以看到默认的前后缀!

​ 因此我们只需要把html页面放到类路径下的templates下,thymeleaf就可以帮我们自动渲染了。

​ 使用Thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!

测试

测试步骤:

  1. 编写一个TestController

    @Controller
    public class TestController {
    @RequestMapping("/test")
    public String test(){
    return "test";
    }
    }
  2. 编写一个测试页面 test.html放在templates目录下

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <h1>test</h1>
    </body>
    </html>
  3. 启动项目请求测试

小结:

  1. 如果使用thymeleaf,只需要导入对应的依赖即可!

  2. html放在templates目录下。

    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";

Thymeleaf语法学习

​ 要学习语法,参考官网文档为准,我们找到对应的版本看看:

【做一个简单的练习:我们需要查出一些数据,在页面中展示】步骤如下:

  1. 修改测试请求,增加数据传输

    model.addAttribute("msg","hello thymeleaf");
  2. 我们要使用thymeleaf,需要在html文件中导入命名空间的约束,方便提示。我们可以去官方文档#3看一下命名空间:

    <html xmlns:th="http://www.thymeleaf.org">
  3. 编写前端页面

    <div th:text="${msg}">
    
    </div>
  4. 启动测试

Thymeleaf的使用语法:

  1. 我们可以使用任意的th:attr来替换html中原生属性的值

  2. 我们可以写哪些表达式?

练习测试

  1. 编写一个controller,放一些数据

    @Controller
    public class TestController {
    @RequestMapping("/test")
    public String test(Model model) {
    model.addAttribute("msg","<h1>hello thymeleaf</h1>");
    model.addAttribute("users", Arrays.asList("111","222","333"));
    return "test";
    }
    }
  2. 测试页面取出数据

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <h1>test</h1>
    <!--所有的html元素都可以被thymeleaf替换接管,th:+元素名,如th:class="" -->
    <div th:text="${msg}"></div>
    <div th:utext="${msg}"></div>
    <hr>
    <!--建议第一种写法-->
    <h3 th:each="user : ${users}" th:text="${user}"></h3>
    <hr>
    <h3 th:each="user : ${users}" >[[${user}]]</h3> </body>
    </html>
  3. 启动项目测试!

记住一句话:

​ 需要使用什么,根据官方文档来查询,才是最重要的,要熟练使用官方文档!

SpringBoot-06-模板引擎Thymeleaf的更多相关文章

  1. SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器

    目录 模板引擎 thymeleaf ContentNegotiatingViewResolver 格式化转换器 模板引擎 thymeleaf.ContentNegotiatingViewResolve ...

  2. springboot:Java模板引擎Thymeleaf介绍

    Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用 ...

  3. Spring Boot (四)模板引擎Thymeleaf集成

    一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...

  4. 新一代Java模板引擎Thymeleaf

    新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...

  5. 8.SpringBoot 模板引擎 Thymeleaf

    1.模板引擎原理 JSP.Velocity.Freemarker.Thymeleaf 都是模板引擎.SpringBoot推荐的Thymeleaf:语法更简单,功能更强大: Thymeleaf模板引擎 ...

  6. springboot集成模板引擎freemarker和thymeleaf

    freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...

  7. SpringBoot系列:Spring Boot使用模板引擎Thymeleaf

    一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...

  8. SpringBoot入门系列(四)整合模板引擎Thymeleaf

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...

  9. SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)

    Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...

  10. 模板引擎Thymeleaf

    1.Thymeleaf简介 Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点 Thyme ...

随机推荐

  1. 【已学完】UGUI Schedule

    章节 内容 签到 Unity4.6 New UI(UGUI) 1.UGUI概述与Canvas画布介绍(一) 5月14日 2.Canvas画布介绍(二) 5月14日 3.Text控件 5月14日 4.I ...

  2. 简介&目录

    欢迎来到 MK 的博客鸭~ 这里会被我用来发一些OI算法.数据结构的学习笔记,各种游记和其他的一些内容,希望大家多多关照! ε≡٩(๑>₃<)۶ 然后目录就也放这里⑧:

  3. C# 转化成 json ,特殊字符的处理

    //1.定义string jsonText0= "{\"beijing\":{\"zkkke\":\"2222\",\" ...

  4. Nginx之https配置

    14.1. 对称加密 安全隐患:钥匙除我之外,还有多个人拥有.泄露风险较大,钥匙传递的过程风险较大 14.2. 非对称加密 优缺点:私钥很安全.但是非对称算法开销很大,大批量应用于业务,会导致性能成本 ...

  5. spring cloud 通过zuul网关去请求的时候报404的几个原因。

    spring cloud 中 zuul 网关的那些坑: 1.检查你的服务是否正常启动. 2.检查你的服务是否正常注册到注册中心. 3.zuul网关的路由规则是会把你注册在注册中心的serviceId ...

  6. 操作DOM对象

    操作DOM对象 目录 操作DOM对象 1. 核心 2. 获得Dom节点 3. 更新节点 1. 操作文本 2. 操作CSS 4. 删除节点 5. 插入节点 1. 追加(将已存在的节点移到后面) 2. 创 ...

  7. 浅谈在win server2012 R2操作系统上安装mysql odbc数据源遇到的问题 -九五小庞

    一,服务器系统 Windows Server 2012 R2 二,安装odbc数据源出现的问题 三,步骤二 中的问题,是因为缺少微软常用运行库.需要安装一下运行库 四,安装odbc数据源 安装MySQ ...

  8. 【原创】解BUG-xenomai内核与linux内核时间子系统之间存在漂移

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 一.问题起源 何为漂移?举个例子两颗32.768kH ...

  9. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  10. 线程的阻塞 sleep() wait() yield()

    为了解决对共享存储区的访问冲突,Java 引入了同步机制,现在让我们来考察多个线程对共享资源的访问,显然同步机制已经不够了,因为在任意时刻所要求的资源不一定已经准备好了被访问,反过来,同一时刻准备好了 ...