注:雖然現在更流行前後端分離,但是個人項目和小型項目開發和外包還是很適合mvc的一個開發模式。而且spring推薦的是thymeleaf,它和shoir、spring-security的整合配合度比較高。所以我們還是使用thymeleaf做爲需要使用時的優先選擇。
-
增加依賴
- 代碼:
<!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
這裏使用springboot就不需要我們去指定版本,springboot可以指定默認的版本。
-
書寫自動裝配的配置
- 代碼:
spring: thymeleaf: suffix: .html prefix: classpath:/templates/ mode: HTML enabled: true cache: false check-template: true encoding: UTF-8
-
修改html
- 增加聲明
<html lang="zh" xmlns:th="<http://www.thymeleaf.org>">
完成。
這裏只是簡單的進行整合。thymeleaf的語法可以看我的博客。
也可以查看官方文檔,我也是從官方文檔中學出來的。