1. 程式人生 > >Spring Boot學習筆記-Thymeleaf模板引擎的配置

Spring Boot學習筆記-Thymeleaf模板引擎的配置

寫在前面

  在開發過程中,使用模板引擎是很有必要的。我之前學習SSM框架,一直是使用Freemarker作為模板引擎,現在學習了Spring Boot之後,知道Spring Boot推薦使用的模板引擎是Thymeleaf,那麼,對於我這種有點輕微強迫症的人來說,肯定是想趕快配置一個Thymeleaf模板引擎。經過查閱資料,配置好後,來發一篇部落格記錄一下配置過程。

我的Spring Boot版本

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId
>
spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> <relativePath/> </parent>

新增依賴

  在專案的pom.xml新增如下依賴:

    <!-- 新增thymeleaf依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- 注意,這裡不需要版本號,因為如果是按照我之前的方法建立的專案,pom.xml檔案裡會自動新增parent結點。-->

配置thymeleaf

  thymeleaf的配置可以在專案的application.ymlapplication.properties中配置,我這裡是選擇在application.yml中配置。它的預設配置就已經滿足我的需求了,我在這裡主要是關閉了快取,因為在開發過程中,如果開啟快取的話,你即使給Spring Boot

配置了熱部署(下篇部落格記錄怎麼配置熱部署),你修改html之後,仍然需要重啟服務,才能看到修改後的頁面,所以,強烈建議在開發過程中關閉快取。

      spring:
      thymeleaf:
        cache: false
    #    prefix: classpath:/templates/
    #    suffix: .html
    #    mode: HTML5
    #    encoding: UTF-8
    #    content-type: text/html
    #    註釋的部分是Thymeleaf預設的配置,如有其它需求可以自行更改

測試模板引擎

  首先,我們在src/main/resources下的templates資料夾下新建hello.html,內容如下:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>
        <font color="red">Hello Thymeleaf.</font>
    </body>
    </html>

  我在使用STSEclipse建立這個檔案的時候,預設meta標籤沒有閉合,訪問可能會報錯。如果報錯,請手動閉合未閉合標籤。
  報錯資訊如下:

org.xml.sax.SAXParseException: The element type “meta” must be terminated by the matching end-tag “</meta>”.

  在我的參考資料中,說:Thymeleaf 3.0之前要求強制閉合,3.0+版本則不要求強制閉合。
  新建一個HelloController的控制器類,程式碼如下:

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;

    /**
     * 這裡使用@Controller,而不是@RestController註解
     * 因為@RestController,表示同時使用@Controller和@ResponseBody,所以會返回hello
     * 而不是返回hello.html的內容。
     * @author howieli
     *
     */
    @Controller
    public class HelloController {

        @GetMapping(value = "/hello")
        public String helloGet() {
            return "hello";
        }

    }

  啟動應用,訪問http://localhost:8080/hello,顯示如下頁面,表示配置成功。
配置成功

傳值

  傳值這一塊只做簡單的記錄,因為現在流行前後端分離。
  HelloController控制器修改如下:

    import java.util.Map;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;

    @Controller
    public class HelloController {

        @GetMapping(value = "/hello")
        public String helloGet(Map<String, Object> map) {
            map.put("name", "HowieLi");
            return "hello";
        }

    }

修改hello.html,通過EL表示式取值:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>
        <font color="red">Hello Thymeleaf.</font>
        <hr />
        Welcome,<span th:text="${name}"></span>
    </body>
    </html>

重新啟動應用,訪問http://localhost:8080/hello,可以看到如下頁面:
hello

結語

  其實配置很簡單,知道一種模板引擎配置方法,就可以照貓畫虎的配置其它模板引擎,如:Freemarker。不過,不管配置哪個模板引擎,建議都關閉快取,這樣修改頁面程式碼,就無需重新啟動Spring Boot,省時省力。至於修改程式碼等等關於熱部署的配置,我的下一篇部落格會記錄我的配置過程。
  個人部落格:https://www.howieli.cn 和個人CSDN部落格: http://blog.csdn.net/howieli_1995

參考資料