springboot訪問template下的html頁面


一、template下文件不允許直接訪問

1、查資料得知:springboot項目默認是不允許直接訪問template下的文件的,是受保護的。

     所以想訪問template下的html頁面,我們可以配置視圖解析器。

2、如果想要用視圖去展示,應該要設置好視圖展示頁面,比如說用一個模板語言來接收返回的數據(thymeleaf或者freemarker等), 也可以用jsp接收,但是SpringBoot官方是不推薦用jsp的,而是建議使用thymeleaf作為模板語言,這里我以thymeleaf為例。

二、配置步驟

1、pom.xml添加依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、application.yml中添加配置

spring:
  thymeleaf:
    prefix:
      classpath: /templates   # 訪問template下的html文件需要配置模板,映射
    cache: false # 開發時關閉緩存,不然沒法看到實時頁面

3、template下添加一個index.html文件

4、后台action配置映射關系

這里有兩種方法,經過嘗試都可以訪問 index.html 頁面

三、結果展示

1、訪問index1,返回到index.html頁面了

2、訪問index2,訪問到html頁面了

只不過,我這里沒有返回數據,所以列表沒有數據,但是返回到頁面了

原文鏈接:https://blog.csdn.net/qq_35161328/article/details/93364230


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM