springboot2.x整合thymeleaf

注:雖然現在更流行前後端分離,但是個人項目和小型項目開發和外包還是很適合mvc的一個開發模式。而且spring推薦的是thymeleaf,它和shoir、spring-security的整合配合度比較高。所以我們還是使用thymeleaf做爲需要使用時的優先選擇。

  1. 增加依賴

    1. 代碼:
    <!-- thymeleaf -->
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    這裏使用springboot就不需要我們去指定版本,springboot可以指定默認的版本。

  2. 書寫自動裝配的配置

    • 代碼:
    spring:
        thymeleaf:
    	    suffix: .html
    	    prefix: classpath:/templates/
    	    mode: HTML
    	    enabled: true
    	    cache: false
    	    check-template: true
    	    encoding: UTF-8
    
  3. 修改html

    1. 增加聲明
    <html lang="zh" xmlns:th="<http://www.thymeleaf.org>">
    

完成。

這裏只是簡單的進行整合。thymeleaf的語法可以看我的博客。

語法

也可以查看官方文檔,我也是從官方文檔中學出來的。

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