1. 程式人生 > >SpringBoot第二篇:web(基於Thymeleaf模板)

SpringBoot第二篇:web(基於Thymeleaf模板)

接著第一篇,繼續配置web專案。

1、在pom檔案中加入:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

spring-boot-starter-thymeleaf:

  Spring Boot提供了預設配置的模板引擎主要有以下幾種:

  1. Thymeleaf
  2. FreeMarker
  3. Velocity
  4. Groovy
  5. Mustache 

Spring Boot建議使用這些模板引擎,避免使用JSP。

  • Thymeleaf是一個XML/XHTML/HTML5模板引擎,可用於Web與非Web環境中的應用開發。它是一個開源的Java庫,基於Apache License 2.0許可,由Daniel Fernández(Java加密庫Jasypt的作者)建立。
  • Thymeleaf提供了一個用於整合Spring MVC的可選模組,在應用開發中,可以使用Thymeleaf來完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標在於提供一種可被瀏覽器正確顯示的、格式良好的模板建立方式,因此也可以用作靜態建模。Thymeleaf 在有網路和無網路的環境下皆可執行,即它可以在瀏覽器檢視頁面的靜態效果,也可以在伺服器檢視帶資料的動態頁面效果,這樣非常有利於前後端的分離。這是由於它支援 html 原型,然後在 html 標籤裡增加額外的屬性來達到模板+資料的展示方式。瀏覽器解釋 html 時會忽略未定義的標籤屬性,所以 thymeleaf 的模板可以靜態地執行;當有資料返回到頁面時,Thymeleaf 標籤會動態地替換掉靜態內容,使頁面動態顯示。
  • Thymeleaf 開箱即用的特性。它提供標準和spring標準兩種方言,可以直接套用模板實現JSTL、 OGNL表示式效果,避免每天套模板、該jstl、改標籤的困擾。同時開發人員也可以擴充套件和建立自定義的方言。
  • Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美整合的可選模組,可以快速的實現表單繫結、屬性編輯器、國際化等功能。

spring boot建議的模板引擎預設的模板配置路徑為:src/main/resources/templates

也可以修改模板配置路徑(在src/main/resources/application.properties檔案中修改

):

#在構建URL時可以預覽檢視名稱的字首。注意結尾斜槓後不許存在字元,空格也不許存在。否則跳轉頁面會出錯又看不出來

spring.thymeleaf.prefix=classpath:/templates/

# 在構建URL時附加到檢視名稱的字尾。

spring.thymeleaf.suffix=.html

模板的其他配置如下:

# Enable template caching.啟用模板快取。
spring.thymeleaf.cache=false 
# Check that the templates location exists.檢查模板位置是否存在。
spring.thymeleaf.check-template-location=true 
# Content-Type value.內容型別值。
spring.thymeleaf.servlet.content-type=text/html 
# Enable MVC Thymeleaf view resolution.啟用MVC TThymeleaf檢視解析。
spring.thymeleaf.enabled=true 
# Template encoding.模板編碼。
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.從解析中排除的檢視名稱的逗號分隔列表。
spring.thymeleaf.excluded-view-names=
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.模板模式應用於模板。也請參閱標準模板。LEGACYHTML5
spring.thymeleaf.mode=LEGACYHTML5 
# Prefix that gets prepended to view names when building a URL.在構建URL時可以預覽檢視名稱的字首。
spring.thymeleaf.prefix=classpath:/templates/
# Suffix that gets appended to view names when building a URL.在構建URL時附加到檢視名稱的字尾。
spring.thymeleaf.suffix=.html

2、使用Thymeleaf模板

      在src/main/resources下建立templates資料夾(預設在此位置,用來存放模板頁面html)

      在templates下建立一個html,如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 align="center"><span th:text="${test}"></span> Hello World</h1>
</body>
</html>

   在src/main/resources下建立static資料夾(預設在此位置,從來存放靜態資原始檔,如:js、css、圖片等)

          在static下放置一個圖片檔案(test.jpg),啟動專案可以直接訪問:http://localhost:8080/test.jpg

3、在PageController.java中新增:

@RequestMapping("/page")
public String index(ModelMap map) {
    // 加入一個屬性,用來在模板中讀取
    map.addAttribute("test", "spring boot");
    // return模板檔案的名稱,對應src/main/resources/templates/page.html
    return "page";
}

4、啟動專案,瀏覽器訪問:http://localhost:8080/page,如圖: