zoukankan      html  css  js  c++  java
  • SpringBoot之Thymeleaf模板引擎

    一、Thymeleaf使用和用法

      1、导入依赖

    1 <dependency>
    2     <groupId>org.thymeleaf</groupId>
    3     <artifactId>thymeleaf-spring5</artifactId>
    4 </dependency>
    5 <dependency>
    6     <groupId>org.thymeleaf.extras</groupId>
    7     <artifactId>thymeleaf-extras-java8time</artifactId>
    8 </dependency>

      2、通过源码可以知道,只要我们将html页面放在"classpath:/templates/" thymeleaf就能自动渲染

    @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";
    }

      3、使用:

        1、导入名称命名空间 

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

        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
    16     same way as they would be obtained using #{…} syntax.
    17     #uris : methods for escaping parts of URLs/URIs
    18     #conversions : methods for executing the configured conversion service (if any).
    19     #dates : methods for java.util.Date objects: formatting, component extraction, etc.
    20     #calendars : analogous to #dates , but for java.util.Calendar objects.
    21     #numbers : methods for formatting numeric objects.
    22     #strings : methods for String objects: contains, startsWith, prepending/appending, etc.
    23     #objects : methods for objects in general.
    24     #bools : methods for boolean evaluation.
    25     #arrays : methods for arrays.
    26     #lists : methods for lists.
    27     #sets : methods for sets.
    28     #maps : methods for maps.
    29     #aggregates : methods for creating aggregates on arrays or collections.
    30     #ids : methods for dealing with id attributes that might be repeated (for example, as a
    31     result of an iteration).
    32 Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
    33   补充:配合 th:object="${session.user}:
    34     <div th:object="${session.user}">
    35       <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
    36       <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
    37       <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
    38     </div>
    39 Message Expressions: #{...}:获取国际化内容
    40 Link URL Expressions: @{...}:定义URL;
    41   @{/order/process(execId=${execId},execType='FAST')}
    42 Fragment Expressions: ~{...}:片段引用表达式
    43   <div th:insert="~{commons :: main}">...</div>
    44 Literals(字面量)
    45   Text literals: 'one text' , 'Another one!' ,…
    46   Number literals: 0 , 34 , 3.0 , 12.3 ,…
    47   Boolean literals: true , false
    48   Null literal: null
    49   Literal tokens: one , sometext , main ,…
    50 Text operations:(文本操作)
    51   String concatenation: +
    52   Literal substitutions: |The name is ${name}|
    53 Arithmetic operations:(数学运算)
    54   Binary operators: + , ‐ , * , / , %
    55   Minus sign (unary operator): ‐
    56 Boolean operations:(布尔运算57   Binary operators: and , or
    58   Boolean negation (unary operator): ! , not
    59 Comparisons and equality:(比较运算)
    60   Comparators: > , < , >= , <= ( gt , lt , ge , le )
    61   Equality operators: == , != ( eq , ne )
    62 Conditional operators:条件运算(三元运算符)
    63   If‐then: (if) ? (then)
    64   If‐then‐else: (if) ? (then) : (else)
    65   Default: (value) ?: (defaultvalue)
    66 Special tokens: 
    67   No‐Operation: _ 没有操作  比如三元运算符  不符条件没有操作 就可以在else后面加_

     二、thymeleaf公共元素抽取

      1、抽取公共片段

    <div th:fragment="copy">
        &copy; 2011 The Good Thymes Virtual Grocery
    </div>

      2、引入公共片段 

    1 <div th:insert="footer :: copy"></div>  将公共片段整个插入到声明引入的元素中
    2 <div th:replace="footer :: copy"></div> 将声明引入的元素替换为公共片段
    3 <div th:include="footer :: copy"></div> 将被引入的片段的内容包含进这个标签中  

      3、三种引用方式效果如下

     1 <div>
     2     <footer>
     3         &copy; 2011 The Good Thymes Virtual Grocery
     4     </footer>
     5 </div>
     6 
     7 <footer>
     8         &copy; 2011 The Good Thymes Virtual Grocery
     9 </footer>
    10 
    11 <div>
    12     &copy; 2011 The Good Thymes Virtual Grocery
    13 </div>
  • 相关阅读:
    基于mAppWidget实现手绘地图--索引&DEMO
    C语言数据结构----栈的定义及实现
    libvirt命令行文档
    清理系统方法
    Linux 经典电子书共享下载
    使用数组实现队列----《数据结构与算法分析---C语言描述》
    清理系统垃圾
    epoll的内部实现 & 百万级别句柄监听 & lt和et模式非常好的解释
    进程、线程、socket套接字-资源大小 & 切换代价
    网络编程学习-面向工资编程
  • 原文地址:https://www.cnblogs.com/lxy-java/p/12993965.html
Copyright © 2011-2022 走看看