Thymeleaf
模板引擎

前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。

jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。

那不支持jsp,如果我们直接用纯静态页面的方式,那给我们开发会带来非常大的麻烦,那怎么办呢?

SpringBoot推荐你可以来使用模板引擎:

模板引擎,我们其实大家听到很多,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他们的思想都是一样的,什么样一个思想呢我们来看一下这张图:
模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,就是我们在后台封装一些数据。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎,不管是jsp还是其他模板引擎,都是这个思想。只不过呢,就是说不同模板引擎之间,他们可能这个语法有点不一样。其他的我就不介绍了,我主要来介绍一下SpringBoot给我们推荐的Thymeleaf模板引擎,这模板引擎呢,是一个高级语言的模板引擎,他的这个语法更简单。而且呢,功能更强大。

我们呢,就来看一下这个模板引擎,那既然要看这个模板引擎。首先,我们来看SpringBoot里边怎么用。
引入Thymeleaf

怎么引入呢,对于springboot来说,什么事情不都是一个start的事情嘛,我们去在项目中引入一下。给大家三个网址:

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

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

Spring官方文档:找到我们对应的版本

https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter

找到对应的pom依赖:可以适当点进源码看下本来的包!

   <dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId></dependency><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-java8time</artifactId></dependency>

Maven会自动下载jar包,我们可以去看下下载的东西;

查看源码
Thymeleaf分析

前面呢,我们已经引入了Thymeleaf,那这个要怎么使用呢?

我们首先得按照SpringBoot的自动配置原理看一下我们这个Thymeleaf的自动配置规则,在按照那个规则,我们进行使用。

我们去找一下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";/*** Whether to check that the template exists before rendering it.*/private boolean checkTemplate = true;/*** Whether to check that the templates location exists.*/private boolean checkTemplateLocation = true;/*** Prefix that gets prepended to view names when building a URL.*/private String prefix = DEFAULT_PREFIX;/*** Suffix that gets appended to view names when building a URL.*/private String suffix = DEFAULT_SUFFIX;/*** Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.*/private String mode = "HTML";/**`在这里插入代码片`* Template files encoding.*/private Charset encoding = DEFAULT_ENCODING;/*** Whether to enable template caching.*/private boolean cache = true;


我们可以在其中看到默认的前缀和后缀!

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

使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!
测试
1、编写一个Controller

package com.xuyuan.springboot03web.Controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class testController {@RequestMapping("/t1")public String test1(){return "test";}
}

2、编写一个测试页面 test.html 放在 templates 目录下

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
template 跳轉發
</body>
</html>

Thymeleaf 语法学习

要学习语法,还是参考官网文档最为准确,我们找到对应的版本看一下;

Thymeleaf 官网:https://www.thymeleaf.org/ , 简单看一下官网!我们去下载Thymeleaf的官方文档!

我们做个最简单的练习 :我们需要查出一些数据,在页面中展示

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

package com.xuyuan.springboot03web.Controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class testController {@RequestMapping("/t1")public String test1(Model model){model.addAttribute("msg","你好徐源");return "test";}
}

2、我们要使用thymeleaf,需要在html文件中导入命名空间的约束,方便提示。

我们可以去官方文档的#3中看一下命名空间拿来过来:

 xmlns:th="http://www.thymeleaf.org"

3、我们去编写下前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div th:text="${msg}"></div>
</body>
</html>


OK,入门搞定,我们来认真研习一下Thymeleaf的使用语法!

1、我们可以使用任意的 th:attr 来替换Html中原生属性的值!
2、我们能写哪些表达式呢?

Simple expressions:(表达式语法)
Variable Expressions: ${...}:获取变量值;OGNL;1)、获取对象的属性、调用方法2)、使用内置的基本对象:#18#ctx : the context object.#vars: the context variables.#locale : the context locale.#request : (only in Web Contexts) the HttpServletRequest object.#response : (only in Web Contexts) the HttpServletResponse object.#session : (only in Web Contexts) the HttpSession object.#servletContext : (only in Web Contexts) the ServletContext object.3)、内置的一些工具对象:#execInfo : information about the template being processed.#uris : methods for escaping parts of URLs/URIs#conversions : methods for executing the configured conversion service (if any).#dates : methods for java.util.Date objects: formatting, component extraction, etc.#calendars : analogous to #dates , but for java.util.Calendar objects.#numbers : methods for formatting numeric objects.#strings : methods for String objects: contains, startsWith, prepending/appending, etc.#objects : methods for objects in general.#bools : methods for boolean evaluation.#arrays : methods for arrays.#lists : methods for lists.#sets : methods for sets.#maps : methods for maps.#aggregates : methods for creating aggregates on arrays or collections.
==================================================================================Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;Message Expressions: #{...}:获取国际化内容Link URL Expressions: @{...}:定义URL;Fragment Expressions: ~{...}:片段引用表达式Literals(字面量)Text literals: 'one text' , 'Another one!' ,…Number literals: 0 , 34 , 3.0 , 12.3 ,…Boolean literals: true , falseNull literal: nullLiteral tokens: one , sometext , main ,…Text operations:(文本操作)String concatenation: +Literal substitutions: |The name is ${name}|Arithmetic operations:(数学运算)Binary operators: + , - , * , / , %Minus sign (unary operator): -Boolean operations:(布尔运算)Binary operators: and , orBoolean negation (unary operator): ! , notComparisons and equality:(比较运算)Comparators: > , < , >= , <= ( gt , lt , ge , le )Equality operators: == , != ( eq , ne )Conditional operators:条件运算(三元运算符)If-then: (if) ? (then)If-then-else: (if) ? (then) : (else)Default: (value) ?: (defaultvalue)Special tokens:No-Operation: _

1、 我们编写一个Controller,放一些数据

package com.xuyuan.springboot03web.Controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;@Controller
public class testController {@RequestMapping("/t1")public String test1(Model model){model.addAttribute("msg","你好徐源");return "test";}@RequestMapping("/t2")public String test2(Map<String,Object>map){map.put("msg","<h1>Helloxuyuan</h1>");
map.put("users", Arrays.asList("xuyuan","answer"));
return "test";}
}

2、测试页面取出数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div th:text="${msg}"></div>
<!--不转义-->
<div th:utext="${msg}"></div><!--遍历数据-->
<!--th:each每次遍历都会生成当前这个标签:官网#9-->
<h3 th:each="user :${users}"th:text="${users}"></h3><h4><!--行内写法:官网#12--><span th:each="user:${users}">[[${user}]]</span>
</h4></body>
</html>


3、启动项目测试!

我们看完语法,很多样式,我们即使现在学习了,也会忘记,所以我们在学习过程中,需要使用什么,根据官方文档来查询,才是最重要的,要熟练使用官方文档!

SpringBoot-08模板引擎 Thymeleaf 找源码官方文档使用 语法的学习相关推荐

  1. Rust模板引擎Tera中文英文对照官方文档

    来嘞早不如来的巧,刚翻译好,你就来啦!翻译完成 要开发CMS(内容管理系统)得有个模板引擎,Tera是使用Rust编写的模板引擎,语法跟JinJa2很像. 引入Tera 要使用Tera只需要在 Car ...

  2. springboot mybatis大学生校园宿舍管理系统源码含文档

    摘 要:宿舍是大学生学习与生活的主要场所之一,宿舍管理是高校学工管理事务中 尤为重要的一项.随着我国高校招生规模的进一步扩大,学生总体人数的不断增加, 宿舍管理工作变得愈加沉重和琐碎,学生宿舍信息的采 ...

  3. Springboot自动售货机后台管理系统源码带文档

    Springboot自动售货机管理系统源码带文档 Springboot自助售货管理系统源码主要从员工.维修人员和补货人员三个角色进行设计,以下分别说明了三类角色的不同功能和访问权限. 1.Spring ...

  4. SpringBoot中模板引擎thymeleaf

    首先我们用SpringBoot创建一个支持thymeleaf的web项目 添加web支持 添加thymeleaf模板引擎 创建好该项目之后,在templates目录下创建一个普通的html文件,这个时 ...

  5. SpringBoot 整合模板引擎 Thymeleaf 页面跳转失败的解决方案

    1. 出错现象 我们知道 Thymeleaf 模板引擎规定的文件路劲应该是在 templates 目录下面的. 但是笔者在开发的过程中,满足了上述要求但是还是没能映射成功. 我们来看看 index.h ...

  6. 基于springboot学生公寓管理系统-计算机毕业设计源码+LW文档

    摘要:本学生公寓管理系统是针对目学生公寓管理的实际需求,从实际工作出发,对过去的学生公寓管理系统存在的问题进行分析,完善学生的使用体会.采用计算机系统来管理信息,取代人工管理模式,查询便利,信息准确率 ...

  7. JavaWeb~模板引擎Thymeleaf总结

    文章目录 模板引擎是什么 有哪些常见的模板引擎 Thymeleaf使用流程 常用标签 链接表达式@{...} 变量表达式${...} 选择变量表达*{...} 消息表达式#{...}(了解) 回顾:几 ...

  8. re python 引擎_转 python内置正则表达式(re)模块官方文档简要中文版

    学习正则表达式,最好的教材是<精通正则表达式>,而要精通NFA正则表达式,使用了NFA引擎的python正则模块官方文档就是最好的教材,大部分的功能同样在其他使用传统NFA引擎的正则包里受 ...

  9. 六十四、SpringBoot中的模板引擎Thymeleaf

    @Author:Runsen 来源:尚硅谷 下面建议读者学习尚硅谷的B站的SpringBoot视频,我是学雷丰阳视频入门的. 具体链接如下:B站尚硅谷SpringBoot教程 文章目录 使用Sprin ...

最新文章

  1. python编程基础语法-Python编程入门基础语法详解
  2. dev里timeedit控件如何赋值_抽奖程序里的字节跳动模式和时长控制,让抽奖更有仪式感!...
  3. 机器学习笔记(十六)强化学习
  4. uniapp自定义条件编译-定制化产品
  5. 爱泼斯坦事件发酵,MIT师生发起抗议逼迫校长Rafael Reif辞职
  6. 雷林鹏分享:jQuery EasyUI 拖放 - 基本的拖动和放置
  7. Mysql添加用户错误:ERROR 1364 (HY000): Field ‘ssl_cipher‘ doesn‘t have a default value解决方法
  8. java实现socket连接,向指定主机指定端口发送socket数据,并获取响应数据
  9. 通过adb和python直接传输Android截图到电脑(windows适用)
  10. 微信小程序 在wxml写过滤器 脱敏手机号
  11. 什么是静电?什么是ESD?ESD分为几种形式?有哪些测试标准?
  12. Golang开源流媒体服务器(RTMP/RTSP/HLS/FLV等协议)
  13. matlab 离散点求导_如何用matlab求离散型数值的导数
  14. 我们听过智商和情商,但你了解过财商吗?-民兴商学院
  15. linux权限英文,Linux常见英文报错中文翻译(菜鸟必知)
  16. Python 从底层结构聊 Beautiful Soup 4(内置豆瓣最新电影排行榜爬取案例)
  17. Node.js全局对象
  18. 招募 | 香港理工大学Georg Kranz 博士诚招博士
  19. 【数据库E-R图知识点和相关习题(复试真题)】
  20. 如何扩展VMware xp虚拟机磁盘

热门文章

  1. 传播智客学习笔记--L11 Logcat
  2. 对凯斯西储大学的轴承故障信号进行小波变换特征提取
  3. python学习笔记__词频统计
  4. 股神巴菲特:垃圾股变黄金股的魔法
  5. openlayer4中,地图通过extent缩放至范围。
  6. 6.18当前,品牌商一定要监测好这些价格
  7. 统计初级专业资格证知识点
  8. gps有几个轨道面_关于GPS系统你知道的有多少?
  9. PostgreSql 唯一索引,表达式索引,部分索引
  10. 项目进度管理的技能之一——进度计划