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


參考資料

  林祥纖的博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章