Thymeleaf 之 內建對象、定義變數、URL參數及標籤自訂屬性_Thymeleaf

來源:互聯網
上載者:User

如標題所述,這篇文章主要講述Thymeleaf中的內建對象(list解析、日期格式化、數字格式化等)、定義變數、擷取URL的參數和在頁面標籤中自訂屬性的應用。

如果對Thymeleaf的基本使用、maven依賴等不清楚的可以先閱讀我的另一篇文章《Thymeleaf 之 初步使用》。 Controller部份

@Controllerpublic class IndexController {    @GetMapping(value = "index")    public String index(Model model, HttpServletRequest request) {        List<String> datas = new ArrayList<String>();        datas.add("知識林");        datas.add("http://www.zslin.com");        datas.add("393156105");        model.addAttribute("datas", datas);        model.addAttribute("curDate", new Date());        model.addAttribute("money", Math.random()*100);        return "index";    }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

在這個控制器的Model中存放了這樣幾個資料:一個String類型的列表、一個日期對象和一個數值,這些東西在實際應用開發過程中應用非常廣泛,下面具體看一下在Thymeleaf中是如何解析這些資料的。 日期格式化

<span th:text="${#dates.format(curDate, 'yyyy-MM-dd HH:mm:ss')}"></span>
1 1

說明: 使用內建對象dates的format函數即可對日期進行格式化,在format函數中,第一個參數是日期對象,對二兩個參數為日期格式(規則跟SimpleDateFormat一樣)

需要注意的是:

· 內建對象一般都以s結尾,如dates、lists、numbers等

· 在使用內建對象是在對象名前都需要加#號。 數字格式化

<span th:text="${#numbers.formatDecimal(money, 0, 2)}"></span>
1 1

說明: 此樣本表示保留兩位小數位,整數位自動;

<span th:text="${#numbers.formatDecimal(money, 3, 2)}"></span>
1 1

說明: 此樣本表示保留兩位小數位,3位整數位(不夠的前加0) 擷取列表長度

<span th:text="${#lists.size(datas)}"></span>
1 1

說明: 使用#lists.size來擷取List的長度。 擷取URL參數值

<span th:text="${#httpServletRequest.getParameter('page')}"></span>
1 1

說明: 當訪問http://localhost:1105/index?page=5時頁面將會得到page對應的值:5。 定義變數

<div th:with="curPage=${#httpServletRequest.getParameter('page')}">    <h3>當前頁碼:<span th:text="${curPage}"></span></h3></div>
1 2 3 1 2 3

說明: 同樣,當訪問http://localhost:1105/index?page=5時,頁面將顯示:當前頁碼:5,說明用th:with來定義變數,多個用,號隔開,使用範圍在當前標籤內。 自訂標籤屬性

在Thymeleaf中可以使用th:加上標籤的任何屬性進行賦值,但有些時候會遇到自訂的屬性,再用th:加自訂的屬性則會無效。比如:需要對<span>標籤增加objName和objId這樣的屬性,在非Thymeleaf情況下是這樣:

<span objId="1" objName="知識林"></span>
1 1

變數情況是:

<span objId="${obj.id}" objName="${obj.name}"></span>
1 1

在Thymeleaf下則是:

<span th:attr="myDate=${#dates.format(curDate, 'yyyy-mm-dd')}, myMoney=${money}"></span>
1 1

說明: 在頁面上查看原始碼可以看到:<span myMoney="91.6059494319957" myDate="2016-31-02"></span>,說明自訂屬性用:th:attr,多個屬性用,隔開。 內建對象

上面簡單描述了比較常用的dates、lists、numbers這幾個內建對象,在Thymeleaf還有很多的內建對象,像strings也非常常用,用法跟Java.lang.String類的用法一樣。

在Thymeleaf中的內建對象有:

#dates:日期格式化內建對象,具體方法可以參照java.util.Date;

#calendars:類似於#dates,但是是java.util.Calendar類的方法;

#numbers: 數字格式化;

#strings:字串格式化,具體方法可以參照java.lang.String,如startsWith、contains等;

#objects:參照java.lang.Object;

#bools:判斷boolean類型的工具;

#arrays:數組操作的工具;

#lists:列表操作的工具,參照java.util.List;

#sets:Set操作工具,參照java.util.Set;

#maps:Map操作工具,參照java.util.Map;

#aggregates:運算元組或集合的工具;

#messages:操作訊息的工具。

範例程式碼:https://github.com/zsl131/thymeleaf-study/tree/master/study05

本文章來自【知識林】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.