SpringBoot使用thymeleaf模板引擎


(1)、添加pom依賴

  

1         <dependency>
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-thymeleaf</artifactId>
4         </dependency>

 

  *  SpringBoot1.x 默認的thymeleaf版本低,如果要自定義版本,需要在pom properties 覆寫SpringBoot默認的thymeleaf版本號

  

1     <properties>
2         <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
3         <!--thymeleaf3需要布局功能layout2以上版本-->
4         <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
5     </properties>

  

  *  SpringBoot2.x 無需此操作

 

(2)、將Html頁面放至classpath:/templates/下,thymeleaf模板引擎將自動渲染

 

  *thymeleaf視圖修改后,如何實時生效

  在配置文件中禁用thymeleaf緩存

spring.thymeleaf.cache=false

頁面修改完成后Ctrl + F9重新編譯(Idea)

瀏覽器刷新頁面


*在控制器重定向跳轉至靜態資源目錄而非thymeleaf目錄,在配置類中注冊一下轉發映射

registry.addViewController("index/html").setViewName("index");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM