1、引入thymeleaf;

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. 2.1.6
  5. </dependency>
  6. 切换thymeleaf版本
  7. <properties>
  8. <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
  9. <!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
  10. <!-- thymeleaf2 layout1-->
  11. <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
  12. </properties>

2、Thymeleaf使用

  1. @ConfigurationProperties(prefix = "spring.thymeleaf")
  2. public class ThymeleafProperties {
  3. private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
  4. private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
  5. public static final String DEFAULT_PREFIX = "classpath:/templates/";
  6. public static final String DEFAULT_SUFFIX = ".html";
  7. //

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

使用:

1、导入thymeleaf的名称空间

  1. <html lang="en" xmlns:th="http://www.thymeleaf.org">

2、使用thymeleaf语法;

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h1>成功!</h1>
  9. <!--th:text 将div里面的文本内容设置为 -->
  10. <div th:text="${hello}">这是显示欢迎信息</div>
  11. </body>
  12. </html>

3、语法规则

1)、th:text;改变当前元素里面的文本内容;

​ th:任意html属性;来替换原生属性的值

2)、表达式?

  1. Simple expressions:(表达式语法)
  2. Variable Expressions: ${...}:获取变量值;OGNL
  3. 1)、获取对象的属性、调用方法
  4. 2)、使用内置的基本对象:
  5. #ctx : the context object.
  6. #vars: the context variables.
  7. #locale : the context locale.
  8. #request : (only in Web Contexts) the HttpServletRequest object.
  9. #response : (only in Web Contexts) the HttpServletResponse object.
  10. #session : (only in Web Contexts) the HttpSession object.
  11. #servletContext : (only in Web Contexts) the ServletContext object.
  12. ${session.foo}
  13. 3)、内置的一些工具对象:
  14. #execInfo : information about the template being processed.
  15. #messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
  16. #uris : methods for escaping parts of URLs/URIs
  17. #conversions : methods for executing the configured conversion service (if any).
  18. #dates : methods for java.util.Date objects: formatting, component extraction, etc.
  19. #calendars : analogous to #dates , but for java.util.Calendar objects.
  20. #numbers : methods for formatting numeric objects.
  21. #strings : methods for String objects: contains, startsWith, prepending/appending, etc.
  22. #objects : methods for objects in general.
  23. #bools : methods for boolean evaluation.
  24. #arrays : methods for arrays.
  25. #lists : methods for lists.
  26. #sets : methods for sets.
  27. #maps : methods for maps.
  28. #aggregates : methods for creating aggregates on arrays or collections.
  29. #ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
  30. Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
  31. 补充:配合 th:object="${session.user}:
  32. <div th:object="${session.user}">
  33. <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
  34. <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
  35. <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
  36. </div>
  37. Message Expressions: #{...}:获取国际化内容
  38. Link URL Expressions: @{...}:定义URL;
  39. @{/order/process(execId=${execId},execType='FAST')}
  40. Fragment Expressions: ~{...}:片段引用表达式
  41. <div th:insert="~{commons :: main}">...</div>
  42. Literals(字面量)
  43. Text literals: 'one text' , 'Another one!' ,…
  44. Number literals: 0 , 34 , 3.0 , 12.3 ,…
  45. Boolean literals: true , false
  46. Null literal: null
  47. Literal tokens: one , sometext , main ,…
  48. Text operations:(文本操作)
  49. String concatenation: +
  50. Literal substitutions: |The name is ${name}|
  51. Arithmetic operations:(数学运算)
  52. Binary operators: + , - , * , / , %
  53. Minus sign (unary operator): -
  54. Boolean operations:(布尔运算)
  55. Binary operators: and , or
  56. Boolean negation (unary operator): ! , not
  57. Comparisons and equality:(比较运算)
  58. Comparators: > , < , >= , <= ( gt , lt , ge , le )
  59. Equality operators: == , != ( eq , ne )
  60. Conditional operators:条件运算(三元运算符)
  61. If-then: (if) ? (then)
  62. If-then-else: (if) ? (then) : (else)
  63. Default: (value) ?: (defaultvalue)
  64. Special tokens:
  65. No-Operation: _

注意

开发期间模板引擎页面修改以后,要实时生效

1)、禁用模板引擎的缓存

  1. # 禁用缓存
  2. spring.thymeleaf.cache=false

2)、页面修改完成以后ctrl+f9:重新编译;

spring boot整合Thymeleaf的更多相关文章

  1. Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  2. 极简 Spring Boot 整合 Thymeleaf 页面模板

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  3. Spring Boot整合Thymeleaf视图层

    目录 Spring Boot整合Thymeleaf Spring Boot整合Thymeleaf 的项目步骤 Thymeleaf 语法详解 Spring Boot整合Thymeleaf Spring ...

  4. 从零开始的Spring Boot(5、Spring Boot整合Thymeleaf)

    Spring Boot整合Thymeleaf 写在前面 从零开始的Spring Boot(4.Spring Boot整合JSP和Freemarker) https://www.cnblogs.com/ ...

  5. Spring Boot整合 Thymeleaf 模板引擎

    什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...

  6. Spring Boot整合Thymeleaf模板引擎

    什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...

  7. Spring Boot 整合 Thymeleaf 完整 Web 案例

    Thymeleaf 是一种模板语言.那模板语言或模板引擎是什么?常见的模板语言都包含以下几个概念:数据(Data).模板(Template).模板引擎(Template Engine)和结果文档(Re ...

  8. spring boot整合Thymeleaf的那些坑(spring boot 学习笔记之四)

    这里简单记录一下Thymeleaf配置和使用的步骤 1.修改pom文件,添加依赖 <dependency> <groupId>org.springframework.boot& ...

  9. spring boot 整合Thymeleaf模板

    SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...

随机推荐

  1. hdu 1281 棋盘游戏(二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 每行每列作为节点,给可以放车的点将对应的行列连边,最多可放置的车即为建二分图后的最大匹配,跑一边 ...

  2. css3之渐变背景色(linear-gradient)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 第十篇 深入Python的dict和set(一)

  4. SQL Server 用户定义表类型

    用户定义表类型: CREATE TYPE [dbo].[TVP_Location] AS TABLE( [Location] [varchar](50) NOT NULL, [Address] [va ...

  5. ACM-ICPC实验室20.2.22测试-动态规划

    C.田忌赛马 直接贪心做就可以~ #include<bits/stdc++.h> using namespace std; ; int a[maxn],b[maxn]; int main( ...

  6. mac 电脑安装express、npm…… 报 ‘Missing write access to /usr/local/lib/node_modules’错误解决办法

    mac电脑安装express框架.npm…… 报 Missing write access to /usr/local/lib/node_modules 错误 终端输入sudo chown -R $U ...

  7. red hat 报错:apt-get:找不到命令

    Linux有两个系列:一个是RedHat系列,一个是Debian系列. RedHat系列:Redhat.Centos.Fedora等 Debian系列:Debian.Ubuntu等 RedHat 系列 ...

  8. PowerDesigner 16.5安装、激活

    PowerDesigner安装 PowerDesigner激活 PowerDesigner运行

  9. 【代码学习】PYTHON 线程

    一.使用threading模块多线程执行 可以明显看出使用了多线程并发的操作,花费时间要短很多 创建好的线程,需要调用start()方法来启动 #coding=utf-8 import threadi ...

  10. pandas库笔记

    本笔记为自学笔记 1.pandas.DataFrame() 一种保存矩阵的数据格式 grades_df = pd.DataFrame( data={'exam1': [43, 81, 78, 75, ...