springBoot預設模板引擎Thymeleaf簡介
阿新 • • 發佈:2019-02-09
由於springBoot預設的是jar包形式,所以不支援,jsp。因此我們需要模板引擎。
JSP、Velocity、Freemarker、Thymeleaf模板引擎的大致原理:頁面+資料交給模板引擎(寫一個頁面模板,裡面一些值是動態的,我們用表示式形式,表達。例如下圖中的template中的${user},資料來自Data),模板引擎通過資料解析表示式,寫到對應位置。最終顯示出去。
市場上大概有5種,而spring預設選擇Thymeleaf(語法更簡單,功能更強大)
使用:
1、引入thymeleaf;注意(預設的是2.1.6的版本)因此我們需要切換版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.切換版本:注意,thymeleaf的主程式與layout版本的對應關係(3版本對應2)
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 佈局功能的支援程式 thymeleaf3主程式 layout2以上版本 -->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>