當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot—thymeleaf语法规则以及使用
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot—thymeleaf语法规则以及使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、渲染機(jī)制
- 二、導(dǎo)入thymeleaf的名稱空間
- 四、使用thymeleaf
- 五、語法規(guī)則
- 1.屬性及屬性的優(yōu)先級(jí)
- 2.表達(dá)式
一、渲染機(jī)制
@ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties {private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");public static final String DEFAULT_PREFIX = "classpath:/templates/";public static final String DEFAULT_SUFFIX = ".html";以前的SpringMVC會(huì)給我們拼串得到j(luò)sp頁面,而現(xiàn)在
只要我們把HTML頁面放在classpath:/templates/,thymeleaf就能自動(dòng)渲染,依據(jù)來自上面的代碼String DEFAULT_PREFIX = “classpath:/templates/”;
訪問地址如:classpath:/templates/success.html
二、導(dǎo)入thymeleaf的名稱空間
<html lang="en" xmlns:th="http://www.thymeleaf.org">四、使用thymeleaf
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>成功!</h1><!--th:text 將div里面的文本內(nèi)容設(shè)置為 --><div th:text="${hello}">這是顯示歡迎信息</div> </body> </html>五、語法規(guī)則
1.屬性及屬性的優(yōu)先級(jí)
th:text;改變當(dāng)前元素里面的文本內(nèi)容;
th:任意html屬性;還可以替換原生屬性的值
2.表達(dá)式
Simple expressions:(表達(dá)式語法)- Variable Expressions: ${...}:獲取變量值;OGNL;1)、獲取對(duì)象的屬性、調(diào)用方法2)、使用內(nèi)置的基本對(duì)象:#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.3)、內(nèi)置的一些工具對(duì)象:#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: *{...}:選擇表達(dá)式:和${}在功能上是一樣;補(bǔ)充:配合 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: #{...}:獲取國際化內(nèi)容- Link URL Expressions: @{...}:定義URL;@{/order/process(execId=${execId},execType='FAST')}- Fragment Expressions: ~{...}:片段引用表達(dá)式<div th:insert="~{commons :: main}">...</div>- Literals(字面量)Text literals: 'one text' , 'Another one!' ,…Number literals: 0 , 34 , 3.0 , 12.3 ,…Boolean literals: true , falseNull literal: nullLiteral tokens: one , sometext , main ,…- Text operations:(文本操作)String concatenation: +Literal substitutions: |The name is ${name}|- Arithmetic operations:(數(shù)學(xué)運(yùn)算)Binary operators: + , - , * , / , %Minus sign (unary operator): -- Boolean operations:(布爾運(yùn)算)Binary operators: and , orBoolean negation (unary operator): ! , not- Comparisons and equality:(比較運(yùn)算)Comparators: > , < , >= , <= ( gt , lt , ge , le )Equality operators: == , != ( eq , ne )- Conditional operators:條件運(yùn)算(三元運(yùn)算符)If-then: (if) ? (then)If-then-else: (if) ? (then) : (else)Default: (value) ?: (defaultvalue)- Special tokens:No-Operation: _學(xué)習(xí)內(nèi)容來自尚硅谷
總結(jié)
以上是生活随笔為你收集整理的Spring Boot—thymeleaf语法规则以及使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot怎么样引入Thym
- 下一篇: Spring Boot—SpringMV