模板引擎之thymeleaf

面向文檔學習編程,在遇到問題官方解決方案一般是最好的

https://www.thymeleaf.org/


模板引擎負責組裝數據,以另一種形式或外觀展現數據

瀏覽器中的頁面是web模板引擎最終的展現。

PHP本身就是天生的模板引擎,

其他服務端模板引擎有JSP,mustache,FreeMark等。

介紹就到這裏,今天總結的是SpringBoot官方推薦Thymeleaf

更簡單,更強大。

一:靜態資源的映射規則

1.寫頁面就需要css,js等,SpringBoot對靜態資源文件的存放有明確規則。

1)webjars:引入jQuery等靜態文件。

2)指定四個位置

"classpath:/META-INF/resources/", 
"classpath:/resources/",
"classpath:/static/", 
"classpath:/public/" 
"/":當前項目的根路徑

2.歡迎頁:所有路徑下的index.html

3.圖標配置:favicon.ico

4.自定義靜態靜態資源加載路徑

二:加載Thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

由於默認版本過低,使用properties更改版本號

<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 佈局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
<!-- thymeleaf2   layout1-->
<thymeleaf-layout-dialect.version>
    2.2.2
</thymeleaf-layout-dialect.version>

三:使用

1.只要把HTML放在templates,Thymeleaf自動渲染

2.在HTML導入名稱空間(語法提示)

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

四:語法規則

與jsp的對應關係

1.表達式
Variable Expressions: ${...}:獲取變量值;OGNL;
1)、獲取對象的屬性、調用方法
2)、使用內置的基本對象:
#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.

${session.foo}
3)、內置的一些工具對象:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
#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.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).

Selection Variable Expressions: *{...}:選擇表達式:和${}在功能上是一樣;
補充:配合 th:object="${session.user}:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

Message Expressions: #{...}:獲取國際化內容
Link URL Expressions: @{...}:定義URL;
@{/order/process(execId=${execId},execType='FAST')}
Fragment Expressions: ~{...}:片段引用表達式
<div th:insert="~{commons :: main}">...</div>

Literals(字面量)
      Text literals: 'one text' , 'Another one!' ,…
      Number literals: 0 , 34 , 3.0 , 12.3 ,…
      Boolean literals: true , false
      Null literal: null
      Literal 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 , or
    Boolean negation (unary operator): ! , not
Comparisons 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: _ 

D:\maven\org\webjars\bootstrap\3.3.6\bootstrap-3.3.6.jar!\META-INF\resources\webjars\bootstrap\3.3.6\css\bootstrap.css

2.登錄錯誤信息的提示
<p style="color: red;" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
3.抽取公共部分引入

thymeleaf有三種引入頁面方式

​ 抽取 : th:fragment

​ 1) 引入 : th:insert

將整個公共片段插入引入中

​ 2) th:replace

將聲明引入的元素替換

​ 3) th:include

將被引入的片段內容包含進標籤

4.日期格式化
#dates.format(emp.birth,'yyyy-MM-dd HH:mm')

提交日期格式化(默認 / )

spring.mvc.date-format=yyyy-MM-dd

五:利器

<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>

將新增和修改設置爲統一界面

但是在rest風格下,

新增是post,修改是put

所以引入以上代碼實現判斷當前操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章