原文鏈接:https://blog.csdn.net/zmemorys/article/details/87275032
一.不用模板引擎的時候。
這裏列出以下幾種情況進行分析。
1. 情況1
【yml配置】
【controller】
【靜態文件目錄】
【結果】
【結論】:此時訪問的error12.html是static目錄下的 。 spring配置視圖爲的時候 controller只需要 return "error12";
2. 情況2
【yml配置】
【controller】
【靜態文件目錄】:不變
【結果】
【結論】此時訪問的error12.html是static目錄下的 。 spring配置視圖爲的時候 controller只需要 return "/error12";
3. 情況3
【yml配置】
【controller】
【靜態文件目錄】:不變
【結果】
【結論】此時訪問的error12.html是static目錄下的 。 spring配置視圖爲的時候 controller只需要 return "/error12.html";
springboot 默認資源根目錄爲static下 路徑一定要寫對不然出來一堆404還不知道爲啥
關於靜態資源的其他配置https://blog.csdn.net/yiifaa/article/details/78299052
1. “spring.mvc.static-path-pattern”
spring.mvc.static-path-pattern代表的含義是我們應該以什麼樣的路徑來訪問靜態資源,換句話說,只有靜態資源滿足什麼樣的匹配條件,Spring Boot纔會處理靜態資源請求,以官方配置爲例:
# 這表示只有靜態資源的訪問路徑爲/resources/**時,纔會處理請求
spring.mvc.static-path-pattern=/resources/**,
假定採用默認的配置端口,那麼只有請求地址類似於“http://localhost:8080/resources/jquery.js”時,Spring Boot纔會處理此請求,處理方式是將根據模式匹配後的文件名查找本地文件,那麼應該在什麼地方查找本地文件呢?這就是“spring.resources.static-locations”的作用了。
2. “spring.resources.static-locations”
“spring.resources.static-locations”用於告訴Spring Boot應該在何處查找靜態資源文件,這是一個列表性的配置,查找文件時會依賴於配置的先後順序依次進行,默認的官方配置如下:
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
繼續以上面的請求地址爲例,“http://localhost:8080/resources/jquery.js”就會在上述的四個路徑中依次查找是否存在“jquery.js”文件,如果找到了,則返回此文件,否則返回404錯誤。
二.使用thymeleaf模板引擎的時候
步驟1.引入jar包 (當引入這個jar包的時候 默認的靜態根目錄變成了templates)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
步驟二:application.yml只需要加(開發時禁用緩存)
spring:
thymeleaf:
cache: false
步驟三:寫controller
步驟四:templates下放html error12.html
結論:
附上html代碼
備註:
1. controller上的註解注意,不能是 @RestController,否則,接口返回的就直接是“/error12.html”這樣的 String了
2. 參考過其他文章:https://blog.csdn.net/moshowgame/article/details/82869151
參考這個沒有走通,把文件夾調成示例中的 tamplates也不行,可能是少了什麼配置?參考 https://blog.csdn.net/fuck487/article/details/79298556 這個,感覺也沒啥特別需要注意的配置,應該能走通的
其他參考:
https://blog.csdn.net/wuxue_123/article/details/52662145