SpringBoot - thymeleaf模板引擎(七)

  • 常見的模板引擎有JSP印荔、Velocity雷恃、Freemarker疙剑、Thymeleaf
  • SpringBoot推薦使用Thymeleaf宰闰;

一潭陪、引入Thymeleaf啟動(dòng)器依賴颠放、

<dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

如需切換thymeleaf版本:

<properties>

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

</properties>

二伪朽、Thymeleaf使用

  • 這里直接看源碼了
  • 默認(rèn)只要我們把HTML頁(yè)面放在classpath:/templates/帮毁,thymeleaf就能自動(dòng)渲染溜宽;
@ConfigurationProperties(prefix = "spring.thymeleaf")   //與配置文件相關(guān)聯(lián)
public class ThymeleafProperties {

    private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;  //編碼格式

    public static final String DEFAULT_PREFIX = "classpath:/templates/";  //默認(rèn)的讀取路徑

    public static final String DEFAULT_SUFFIX = ".html";  //默認(rèn)的后綴名

1吉拳、創(chuàng)建模板文件t1.html,并導(dǎo)入thymeleaf的名稱空間

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>

2适揉、在controller中準(zhǔn)備數(shù)據(jù)

@RequestMapping("thy")
    public String thy(Map<String,Object> map){
        map.put("til","thymelaf測(cè)試頁(yè)面");
        map.put("ceshi","<h2  style='color: red'>utext會(huì)轉(zhuǎn)義特殊字符留攒,如:html標(biāo)簽</h2>");
        map.put("list", Arrays.asList("zhangshan","lisi","wangwu"));
        return "thymeleaf01";
    }

3煤惩、使用模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>[[${til}]]</title>
</head>
<body>
  <div th:text="${til}">這是一個(gè)頁(yè)面</div>
<div th:utext="${ceshi}"></div>
 <hr/>
  <h2 th:each="item : ${list}">[[${item}]]</h2>
</body>
</html>

三、語(yǔ)法規(guī)則

  • 1炼邀、
    th:text --> 改變當(dāng)前元素里面的文本內(nèi)容魄揉;
    th:任意html屬性 --> 來替換原生屬性的值


    image.png
  • 2、語(yǔ)法表達(dá)式
Simple expressions:(表達(dá)式語(yǔ)法)
    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.
                
                ${session.foo}
            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: #{...}:獲取國(guó)際化內(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 , false
      Null literal: null
      Literal 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 , or
    Boolean 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: _ 

更多配置參考官方文檔:https://www.thymeleaf.org/documentation.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市掸犬,隨后出現(xiàn)的幾起案子袜漩,更是在濱河造成了極大的恐慌,老刑警劉巖湾碎,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宙攻,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡介褥,警方通過查閱死者的電腦和手機(jī)粘优,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呻顽,“玉大人,你說我怎么就攤上這事丹墨±缺椋” “怎么了?”我有些...
    開封第一講書人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵贩挣,是天一觀的道長(zhǎng)喉前。 經(jīng)常有香客問我,道長(zhǎng)王财,這世上最難降的妖魔是什么卵迂? 我笑而不...
    開封第一講書人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮绒净,結(jié)果婚禮上见咒,老公的妹妹穿的比我還像新娘。我一直安慰自己挂疆,他們只是感情好改览,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開白布下翎。 她就那樣靜靜地躺著,像睡著了一般宝当。 火紅的嫁衣襯著肌膚如雪视事。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,457評(píng)論 1 311
  • 那天庆揩,我揣著相機(jī)與錄音俐东,去河邊找鬼。 笑死订晌,一個(gè)胖子當(dāng)著我的面吹牛虏辫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播腾仅,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼乒裆,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了推励?” 一聲冷哼從身側(cè)響起鹤耍,我...
    開封第一講書人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎验辞,沒想到半個(gè)月后稿黄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡跌造,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年杆怕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片壳贪。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡陵珍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出违施,到底是詐尸還是另有隱情互纯,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布磕蒲,位于F島的核電站留潦,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏辣往。R本人自食惡果不足惜兔院,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望站削。 院中可真熱鬧坊萝,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至扯键,卻和暖如春睦袖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荣刑。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工馅笙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人厉亏。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓董习,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親爱只。 傳聞我的和親對(duì)象是個(gè)殘疾皇子皿淋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360