java 獲取日期
阿新 • • 發佈:2020-12-31
1.什麼是Thymeleaf
Thymeleaf其實說白了就是jsp一樣的東西,用來將html翻譯成動態的一個東西,類似的還有freemark等等,因為jsp太老了幾乎要被淘汰了。
注意這裡中間需要一個template引擎也就是用來翻譯的,thymeleaf只是一個模板罷了
2.如何將Thymeleaf引入到springboot專案中
Thymeleaf 官網:https://www.thymeleaf.org/
Thymeleaf 在Github 的主頁:https://github.com/thymeleaf/thymeleaf
Spring官方文件:找到我們對應的版本
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
在上面這個連線中找到下面這個東西點選pom
找到對應的pom依賴:可以適當點進原始碼看下本來的包!
引入其中這兩個依賴(這裡注意要用spring5和jdk8)
Maven會自動下載jar包,我們可以去看下下載的東西;
開啟ThymeleafProperties
建立如下東西:
我們可以去官方文件的#3中看一下名稱空間拿來過來(其實這一步就像引入el標籤庫一樣):
xmlns:th="http://www.thymeleaf.org"
看到沒有這裡就可以直接用el表示式來取了
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>狂神說</title> </head> <body> <h1>測試頁面</h1> <!--th:text就是將div中的內容設定為它指定的值,和之前學習的Vue一樣--> <!--所有的html元素都可以被thymeleaf替換接管: th:元素名--> <div th:text="${msg}"></div> </body> </html>
4.3常用語法
練習一:
controller:
html:
顯示:
練習二:
controller:
html:
顯示:
上面這種遍歷也可以寫成這個樣子:
4.4我們能寫哪些表示式呢?
Simple expressions:(表示式語法)
Variable Expressions: ${...}:獲取變數值;OGNL;
1)、獲取物件的屬性、呼叫方法
2)、使用內建的基本物件:#18
#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)、內建的一些工具物件:
#execInfo : information about the template being processed.
#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.
==================================================================================
Selection Variable Expressions: *{...}:選擇表示式:和${}在功能上是一樣;
Message Expressions: #{...}:獲取國際化內容
Link URL Expressions: