首页 > 编程语言 > 详细

SpringBoot之Thymeleaf模板引擎

时间:2020-05-30 19:25:13      阅读:56      评论:0      收藏:0      [点我收藏+]

一、Thymeleaf使用和用法

 通过源码可以知道,只要我们将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";
}

  使用:

    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后面加_

 

SpringBoot之Thymeleaf模板引擎

原文:https://www.cnblogs.com/lxy-java/p/12993965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!