Springboot+Thymeleaf配置與使用

JSP Tomcat 科技 和尚洗頭愛飄柔 和尚洗頭愛飄柔 2017-08-26

前言

Springboot默認是不支持JSP的,默認使用thymeleaf模板引擎。所以這裡介紹一下springboot使用Thymeleaf的實例以及遇到的問題。

配置與使用

1.在application.properties文件中增加Thymeleaf模板的配置。

Springboot+Thymeleaf配置與使用

說明一下,這些配置不是必須的,如果配置了會覆蓋默認的。在開發時建議將spring.thymeleaf.cache設置為false,否則會有緩存,導致頁面沒法及時看到更新後的效果。比如你修改了一個文件,已經update到tomcat,但刷新頁面還是之前的頁面,就是因為緩存引起的。

2.在pom.xml中添加thymeleaf的依賴

Springboot+Thymeleaf配置與使用

3.編寫Controller

Springboot+Thymeleaf配置與使用

可以看到,Controller與普通的SpringMVC的Controller無異。

4.編寫greeting.html

Springboot+Thymeleaf配置與使用

5.運行效果

Springboot+Thymeleaf配置與使用

這裡這是一個基本的使用示例,更多Thymeleaf的用法請自行搜索。

相關推薦

推薦中...