Thymeleaf学习笔记

一:开启Thymeleaf

初学web框架时候大家都常常使用JSP作为SSM架构的前端渲染,但是越来越多的人选择使用HTML5作为前端开发的主要手段,这时候JSP的前后端分离就显得不是很适合,大家都是用Thymeleaf、freemarker等模板引擎作为前后端分离及渲染的框架。其实专业术语说起来很让你陌生,当转化为普通话的时候就是这样理解的,JSP的优点是什么?跨平台,可扩展,JSTL表达式等等。Thymeleaf就是让你把以.html为后缀的文件当作.jsp为后缀的使用,从而让你回到你所熟悉的SSM开发。
使用Thymeleaf的方法很简单。而现在比较常用的是与SpringBoot结合,只需要三步
1:引入依赖

    使用Maven时候为
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    使用Gradle请使用
    ext ['thymeleaf.version'] = '3.0.3.RELEASE'
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    }

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2:开启spring.thymeleaf

#在配置文件中配置
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
    
    
  • 1
  • 2
  • 3
  • 4
  • 5

跟踪源码,我们发现,在自动配置类中,
这里写图片描述
这个自动配置类配置了

//配置模板解析器的
DefaultTemplateResolverConfiguration-->TemplateResolver
//主要是配置是由什么解析器来解析的,默认配置为HTML
    
    
  • 1
  • 2
  • 3

这里写图片描述
通过
AbstractTemplateResolverConfiguration中的checkTemplateLocationExists()方法,获取前缀,然后去前缀中查找是否由.html后缀的文件。前缀默认配置为classpath:/templates/这也就是我们需要在template文件夹中配置.html文件的原因,当然你也可以动过在配置文件.properties中更改属性spring.thymeleaf.prefix=xxx来更改默认的配置路径。前缀后缀都是在.properties文件中获取的。

//配置Servlet的
Thymeleaf2ViewResolverConfiguration-->Servlet
    
    
  • 1
  • 2

配置解析器Thymeleaf2ViewResolverConfiguration
配置模板引擎: setTemplateEngine(final ITemplateEngine templateEngine)

//配置模板引擎的,当由不同的模板解析器时,负责将模板解析器装载到引擎中。如下图
ThymeleafDefaultConfiguration-->SpringTemplateEngine
    
    
  • 1
  • 2

这里写图片描述

//以下的和我们关系不大,就不详细介绍了
//配置方言的
ThymeleafConditionalCommentsDialectConfiguration-->ConditionalCommentsDialect
 //配置页面布局的
 ThymeleafWebLayoutConfiguration-->LayoutDialect
 //配置DataAttribute方言的
 DataAttributeDialectConfiguration-->DataAttributeDialect
 //配置SpringSecurity方言的
 ThymeleafSecurityDialectConfiguration-->SpringSecurityDialect
 //配置Java8方言的
 ThymeleafJava8TimeDialect-->Java8TimeDialect
 //用来配置拼接映射路径并转换编码的
 ThymeleafResourceHandlingConfig

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3:命名空间

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

二:表达式

1:变量表达式:${}
选择表达式:*{}
消息表达式:#{}
URL表达式:@{}
片段表达式:~{}
2: 文本文字:’one text’,’Another one!’,…
号码文字:0,34,3.0,12.3,…
布尔文字:true,false
空文字: null
文字标记:one,sometext,main,…
3:文字操作:

字符串连接: +
文字替换: |The name is ${name}|
4:算术运算:
二元运算符:+,-,*,/,%
减号(一元运算符): -
5:布尔运算:
二元运营商:and,or
布尔否定(一元运算符): !,not
6:比较和平等:
比较:>,<,>=,<=(gt,lt,ge,le)
平等运营商:==,!=(eq,ne)
7:有条件的操作符:
IF-THEN: (if) ? (then)
IF-THEN-ELSE: (if) ? (then) : (else)
默认: (value) ?: (defaultvalue)
8:特殊令牌:
无操作: _

        <link href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css" rel="stylesheet">
            </div>

一:开启Thymeleaf

初学web框架时候大家都常常使用JSP作为SSM架构的前端渲染,但是越来越多的人选择使用HTML5作为前端开发的主要手段,这时候JSP的前后端分离就显得不是很适合,大家都是用Thymeleaf、freemarker等模板引擎作为前后端分离及渲染的框架。其实专业术语说起来很让你陌生,当转化为普通话的时候就是这样理解的,JSP的优点是什么?跨平台,可扩展,JSTL表达式等等。Thymeleaf就是让你把以.html为后缀的文件当作.jsp为后缀的使用,从而让你回到你所熟悉的SSM开发。
使用Thymeleaf的方法很简单。而现在比较常用的是与SpringBoot结合,只需要三步
1:引入依赖

    使用Maven时候为
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    使用Gradle请使用
    ext ['thymeleaf.version'] = '3.0.3.RELEASE'
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    }

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2:开启spring.thymeleaf

#在配置文件中配置
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

跟踪源码,我们发现,在自动配置类中,
这里写图片描述
这个自动配置类配置了

//配置模板解析器的
DefaultTemplateResolverConfiguration-->TemplateResolver
//主要是配置是由什么解析器来解析的,默认配置为HTML
  
  
  • 1
  • 2
  • 3

这里写图片描述
通过
AbstractTemplateResolverConfiguration中的checkTemplateLocationExists()方法,获取前缀,然后去前缀中查找是否由.html后缀的文件。前缀默认配置为classpath:/templates/这也就是我们需要在template文件夹中配置.html文件的原因,当然你也可以动过在配置文件.properties中更改属性spring.thymeleaf.prefix=xxx来更改默认的配置路径。前缀后缀都是在.properties文件中获取的。

//配置Servlet的
Thymeleaf2ViewResolverConfiguration-->Servlet
  
  
  • 1
  • 2

配置解析器Thymeleaf2ViewResolverConfiguration
配置模板引擎: setTemplateEngine(final ITemplateEngine templateEngine)

//配置模板引擎的,当由不同的模板解析器时,负责将模板解析器装载到引擎中。如下图
ThymeleafDefaultConfiguration-->SpringTemplateEngine
  
  
  • 1
  • 2

这里写图片描述

//以下的和我们关系不大,就不详细介绍了
//配置方言的
ThymeleafConditionalCommentsDialectConfiguration-->ConditionalCommentsDialect
 //配置页面布局的
 ThymeleafWebLayoutConfiguration-->LayoutDialect
 //配置DataAttribute方言的
 DataAttributeDialectConfiguration-->DataAttributeDialect
 //配置SpringSecurity方言的
 ThymeleafSecurityDialectConfiguration-->SpringSecurityDialect
 //配置Java8方言的
 ThymeleafJava8TimeDialect-->Java8TimeDialect
 //用来配置拼接映射路径并转换编码的
 ThymeleafResourceHandlingConfig

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3:命名空间

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

二:表达式

1:变量表达式:${}
选择表达式:*{}
消息表达式:#{}
URL表达式:@{}
片段表达式:~{}
2: 文本文字:’one text’,’Another one!’,…
号码文字:0,34,3.0,12.3,…
布尔文字:true,false
空文字: null
文字标记:one,sometext,main,…
3:文字操作:

字符串连接: +
文字替换: |The name is ${name}|
4:算术运算:
二元运算符:+,-,*,/,%
减号(一元运算符): -
5:布尔运算:
二元运营商:and,or
布尔否定(一元运算符): !,not
6:比较和平等:
比较:>,<,>=,<=(gt,lt,ge,le)
平等运营商:==,!=(eq,ne)
7:有条件的操作符:
IF-THEN: (if) ? (then)
IF-THEN-ELSE: (if) ? (then) : (else)
默认: (value) ?: (defaultvalue)
8:特殊令牌:
无操作: _

        <link href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css" rel="stylesheet">
            </div>

猜你喜欢

转载自blog.csdn.net/qq_22771739/article/details/81669916
今日推荐