Thymeleaf介紹和基本語法使用


Spring Boot中推薦使用Thymeleaf作為模板引擎.因為Thymeleaf提供了完美的SpringMVC支持.

Thymeleaf是一個java類庫,他是一個xml/xhtml/html5的模板引擎,可以作為mvcweb應用的view層。

 

 

 我們把HTML頁面放在classpath:/templates/thymeleaf就能自動渲染;

 

導入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>

 語法規則

 th:text;改變當前元素里面的文本內容

 th:任意html屬性;來替換原生屬性的值

th:include:加載模板的內容: 讀取加載節點的內容(不含節點名稱),替換div內容

th:replace:替換當前標簽為模板中的標簽,加載的節點會整個替換掉加載他的div

th:attr 來設置任意屬性

th:attrprepend 來追加(不是替換)屬性值

th:classappend

th:each每次遍歷都會生成當前這個標簽

 th:href="@{...}" 替換url

 th:text="${...}"     轉譯特殊字符,特殊符號原方不動輸出

th:utext="${...}    會轉譯字符,特殊符號被轉譯后輸出結果

 行內寫法

[[ ]]等價於th:text      

[( )]等價於th:utext 

 

表達式語法

 選擇變量表達式: *{...}

  消息表達式: #{...}

 URL 表達式: @{...}

代碼段表達式: ~{...}

字面量

文本字面量: 'some text'

 數值字面量: 0, 34, 3.0, 12.3

 布爾值字面量: true, false

 Null 值字面量: null

 Tokens 字面量: one, content, sometext, ...

文本操作符

 字符串連接: +

字面量替換: |The name is ${name}|

 算術操作符

 二元操作符: +, -, *, /, %

 減號(一元操作符): -

 布爾操作符(邏輯操作符)

 二元操作符: and, or

 (一元操作符): !, not

 比較操作符

比較: >, <, >=, <= (gt, lt, ge, le)

 相等性: ==, != (eq, ne)

 條件操作符

if-then: (if) ? (then)

if-then-else: '(if) ? (then) : (else)'

默認: (value) ?: (defaultvalue)

特殊符號

 忽略 Thymeleaf 操作: _

內置基本對象

 ctx:

 vars

 locale

 request

 response

 session

 servletContext

內置工具對象

execInfo

messages

uris

conversions

dates

calendars

numbers

strings

objects

bools

arrays

lists

sets

maps

aggregates

ids

Link url

 th:href="@{/}"返回首頁

 

th:href="@{/thymeleaf/demo1}"跳轉demo1 頁面

 

th:href="@{/thymeleaf/demo1(username=${employees[0].name})}">demo1 頁面, 帶參數

 

th:href="@{/thymeleaf/demo1/{empId}(empId=${employees[1].id})}">demo1 頁面, RESTful 風格參數

 

Thymeleaf全部標簽

th:abbr

th:accept

th:accept-charset

th:accesskey

th:action

th:align

th:alt

th:archive

th:audio

th:autocomplete

th:axis

th:background

th:bgcolor

th:border

th:cellpadding

th:cellspacing

th:challenge

th:charset

th:cite

th:class

th:classid

th:codebase

th:codetype

th:cols

th:colspan

th:compact

th:content

th:contenteditable

th:contextmenu

th:data

th:datetime

th:dir

th:draggable

th:dropzone

th:enctype

th:for

th:form

th:formaction

th:formenctype

th:formmethod

th:formtarget

th:frame

th:frameborder

th:headers

th:height

th:high

th:href

th:hreflang

th:hspace

th:http-equiv

th:icon

th:id

th:keytype

th:kind

th:label

th:lang

th:list

th:longdesc

th:low

th:manifest

th:marginheight

th:marginwidth

th:max

th:maxlength

th:media

th:method

th:min

th:name

th:optimum

th:pattern

th:placeholder

th:poster

th:preload

th:radiogroup

th:rel

th:rev

th:rows

th:rowspan

th:rules

th:sandbox

th:scheme

th:scope

th:scrolling

th:size

th:sizes

th:span

th:spellcheck

th:src

th:srclang

th:standby

th:start

th:step

th:style

th:summary

th:tabindex

th:target

th:title

th:type

th:usemap

th:value

th:valuetype

th:vspace

th:width

th:wrap

th:xmlbase

th:xmllang

th:xmlspace

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM