Thymeleaf初體驗

因爲spring boot取消了對velocity模板語言的支持,所以只能換成其餘語言了,如今我選擇Thymeleaf,畢竟官方推薦。html

一上手Thymeleaf很是不習慣,它跟經常使用的Java語法格式相差甚遠。甚至跟HTML語言很是類似。spring

下面是Java項目經常使用的Thymeleaf語法,我一一作了實驗:express

Thymeleaf語言風格

  在進行一些簡單的操做,例如,分支,循環等。Thymeleaf會做爲一種標籤的屬性,在屬性中取值並顯示。這就是它的風格。app

  例如:less

    Controller包中的文件:學習

1 @Controller
2 public class deal {
3     @RequestMapping(path = {"/hello"})
4     public String Hello_World(Model model) {
5         model.addAttribute("aaa", "naive");
6         model.addAttribute("l", "<br/>");
7         return "Hello";
8     }

    Templates包中的模板:this

 1 <html>
 2 <head>
 3     <title>Welcome!</title>
 4 </head>
 5 <body>
 6 <!--/*
 7     fdsfd
 8     <h1>Welcome</h1>
 9 */-->
10     <p th:text=${aaa}></p>
11     <p>th:utext=${aaa}</p>
12     <p th:utext=${aaa}>th:utext=${aaa}</p>
13     //
14     <p th:text=${aaa}></p>
15     <p th:utext=${aaa}></p>
16     <p th:utext=${l}></p>
17 </body>
18 </html>

   顯示結果:spa

 

  總結:只有把一些標籤屬性,例如th:text,th:utext等放到像<p>這樣的標籤內,網頁才能正常顯示Controller文件定義好的變量值。所以,要學好Thymeleaf,須要把標籤學好,須要把HTML學好。。。3d

註釋

  建議用<!--/*    *-->這個,能夠註釋多行。code

1 <!--/*
2     fdsfd
3     <h1>Welcome</h1>
4 */-->

   同時取消註釋也簡單。

1 <!--/*/
2     fdsfd
3     <h1>Welcome</h1>
4 /*/-->

顯示文本

  th:text或者th:utext

1 <p th:text=${aaa}></p>
2 <p>th:utext=${aaa}</p>
3 <p th:utext=${aaa}>th:utext=${aaa}</p>

  顯示結果:

  

   總結:th:text均可以顯示文本,把屬性放到<p></p>標籤裏面,能夠顯示C層變量(第一行)。放到<p></p>中間夾住,不能取值(第二行)。同時標籤中的值會取消夾住地方的值。(第三行)

 

  th:text不能解析標籤,可是th:utext能。以下:

1 model.addAttribute("l", "<br/>");

  使用l變量:

1 <p th:text=&{l}></p>

  出錯:There was an unexpected error (type=Internal Server Error, status=500).Could not parse as expression: "&{l}" (template: "Hello" - line 13, col 6)。它表示th:text沒法解析l變量中的標籤。

 

  換一種寫法:

1     <!--/*-->
2         <p th:text=&{l}></p>
3     <!--*/-->
4     <p th:utext=${l}></p>
5   <p>th:utext=${aaa}</p>

  顯示結果:

  

 

分支結構

  傳送門

   注:4.6節後面,全是乾貨。

單分支1

    若if成立,則顯示<div></div>中間的文本。不然不顯示。th:unless是不成立顯示。

1     <div th:if="${aaa} == false"> 
2         new block
3     </div>

  因爲aaa不爲false,所以new block不會顯示出來。

  若是將false改成'naive'?

1     <div th:if="${aaa} == 'naive'"> 
2         new block
3     </div>

  結果顯示new block文字。

單分支2

<p th:text="${aaa}" th:if="${not #strings.isEmpty(aaa)}"></p>

 

  若if爲true,顯示text部分,不然不顯示。

多分支

  多分支跟其餘語言同樣。使用:?三目運算符。(condition)?(then):(else)

    <p th:text="${aaa}? 'even' : 'odd'"></p>

  當aaa爲真,""裏面是even,不然""裏面是odd。

 

循環結構

  th:each逐個取出liststring中的值,放到n中,最後再一次顯示每一個n。代碼以下:

  Controller包:

1     List<String> ls = Arrays.asList(new String[] {"1","111", "2323a"});
2     model.addAttribute("liststring", ls);

  向Templates包傳遞ls變量。

  Templates包:

1     <div>
2         <p th:text="${n}" th:each="n : ${liststring}"> </p>
3     </div>

   結果顯示:

  

 

 

Thymeleaf調用方法

  單分支中有行代碼:

<p th:text="${aaa}" th:if="${not #strings.isEmpty(aaa)}"></p>

   strings.isEmpty()方法是Thymeleaf中本身實現的方法。

  有時候在模板中須要進行一些複雜的操做,手寫不太現實,Thymeleaf早就給咱們想好了。文檔第19章講的就是Thymeleaf的一些類,能夠使用它們調用須要的方法。

 調用自定義類方法

  在新建的包model中建一個類:

 1 package cn.scu.toutiao.model;
 2 
 3 public class User {
 4     
 5     public User(String name) {
 6         this.name = name;    
 7         }
 8     
 9     public String name;
10     public String setName(String name) {
11         this.name = name;
12         return this.name;
13     }
14 }

  在controller包中傳入這個類的對象:

model.addAttribute("user", new User("aw12"));

  在Templates包中顯示:

1  <p th:text="${user.name}"> </p>
2  <p th:text="${user.setName('fire on fire')}"> </p>

  顯示結果:

 

  

   總結:參數不能用雙引號,要用單引號。方法前不加#。

調用已有類的方法

  類在文檔中,記住在方法前加上#。

 

引入其它文件

  有時候,每一個頁面的頭尾都是同樣的。這時候建立另外一個文件,存放頭部或者尾部,再引入便可。

  例如:在Templates包中新建一個頁面footer.html。

1 <div th:fragment="copy">
2 i am strong man manman hahahah~~~
3 </div>

  在hello.html中引入:

<div th:insert="footer :: copy"> </div>

  就能夠將footer.html文件做爲hello.html文件的一部分顯示了。

  顯示結果:

  

  技術總結:

  1.在被引入的文件中用th:fragment標明能夠被應用的那一部分的名字。

   2.在引入的文件用th:insert表示引入哪一個文件的哪一部分。"文件名 :: 部分名"。

 

 

 

  其它的內容與上同理,如今已經能夠在Thymeleaf中放飛自我了。同時,繼續學習HTML吧。

  o(╯□╰)o ╯▂╰ ╯0╰ ╯︿╰ ╯ω╰ ╯﹏╰ ╯△╰ ╯▽╰ +▂+ +0+ +︿+ +ω+ +﹏+ +△+ +▽+ ˋ▂ˊ

 

  Thymeleaf經常使用語法傳送門

相關文章
相關標籤/搜索