1. 程式人生 > >Springboot中使用thymeleaf模板引擎實現頁面跳轉

Springboot中使用thymeleaf模板引擎實現頁面跳轉

1、建立一個Springboot專案


2、在pom.xml中加入thymeleaf模板引擎的依賴


<!-- springboot web開發thymeleaf模板 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3、在application.yml配置檔案中關閉thymeleaf快取和讀取templates/static資源下的檔案

(我這裡是application.yml檔案改了字尾名,預設是application.properties檔案,兩者寫法格式不一樣,具體自己實踐)


#配置支援thymeleaf模板引擎
thymeleaf:
  cache: false
  prefix: classpath:templates/
  suffix: .html
 #配置靜態資源的位置
mvc:
   static-path-pattern: /**
resources:
   static-locations: classpath:/static/

4、controller中跳轉頁面


/**
 * 訪問登入頁面
 */
@RequestMapping
("") public String defaultpage(){ return "index"; }
5、在templates資料夾下面建立一個html頁面就可以訪問咯