Spring官方支持的服務的渲染模板中,並不包含jsp。而是Thymeleaf和Freemarker等,而Thymeleaf與SpringMVC的視圖技術,及SpringBoot的自動化配置集成很是完美,幾乎沒有任何成本,你只用關注Thymeleaf的語法便可。html
動靜結合:Thymeleaf 在有網絡和無網絡的環境下皆可運行,即它能夠讓美工在瀏覽器查看頁面的靜態效果,也能夠讓程序員在服務器查看帶數據的動態頁面效果。這是因爲它支持 html 原型,而後在 html 標籤裏增長額外的屬性來達到模板+數據的展現方式。瀏覽器解釋 html 時會忽略未定義的標籤屬性,因此 thymeleaf 的模板能夠靜態地運行;當有數據返回到頁面時,Thymeleaf 標籤會動態地替換掉靜態內容,使頁面動態顯示。程序員
開箱即用:它提供標準和spring標準兩種方言,能夠直接套用模板實現JSTL、 OGNL表達式效果,避免天天套模板、該jstl、改標籤的困擾。同時開發人員也能夠擴展和建立自定義的方言。spring
多方言支持:Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美集成的可選模塊,能夠快速的實現表單綁定、屬性編輯器、國際化等功能。瀏覽器
與SpringBoot完美整合,SpringBoot提供了Thymeleaf的默認配置,而且爲Thymeleaf設置了視圖解析器,咱們能夠像之前操做jsp同樣來操做Thymeleaf。代碼幾乎沒有任何區別,就是在模板語法上有區別緩存
pom.xml服務器
<!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
application.yml (encoding:UTF-8解決中文亂碼)網絡
spring:
#開始thymeleaf設置
thymeleaf:
#禁用模板緩存
cache: false
#設置文字消息
messages:
encoding: UTF-8
basename: message_zh_CN
controller:app
@Controller
@RequestMapping("/test")
public class MyThymeleaf {
@GetMapping("/h")
public String t(Model model){
String title="標題";
String message="first thymeleaf !!";
model.addAttribute("message",message);
model.addAttribute("title",title);
return "index";
}
}
index.htmljsp
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> ------ 聲明當前文件是 thymeleaf, 裏面能夠用th開頭的屬性 <head> <meta charset="UTF-8"> <title>首頁</title> </head> <body> <h1 th:text="#{title}"></h1> <h1 th:text="${message}"></h1> </body> </html>
啓動後訪問:http://localhost:8080/test/h編輯器