SpringBoot 2 X 訪問靜態資源

2021-10-05 05:00:40 字數 1038 閱讀 8579

我專案中的檔案如下,使用的是spring boot 2.2.6 ,也就是2.x版本

html檔案

資源目錄

controller層

訪問html的時候,發現靜態資源沒有訪問到,在html檔案中,靜態資源路徑寫的是沒問題的,在springboot1.x的時候也是沒問題的,但是為什麼訪問不到?究其原因是使用的springboot2.x少了乙個配置:主要是那個addresourcehandlers方法,有了它就可以進行靜態資源訪問了,具體是因為什麼?這要看源**背後的原理,這個後來要探索下。

import org.springframework.context.annotation.configuration;

import org.springframework.web.servlet.config.annotation.*;

@configuration

@enablewebmvc

class

mvcconfig

implements

webmvcconfigurer

@override

public

void

addresourcehandlers

(resourcehandlerregistry registry)

spring boot2 x訪問靜態資源

此處對比兩個spring boot版本訪問靜態資源的區別 jdk分別為1.7與1.8 1.5.9.release 2.1.6.release 資源未被攔截 可以訪問 可以訪問 資源被攔截 可以訪問 不可以訪問 由此可見,如果spring boot版本為2.x,那存在配置了靜態資源後不能被訪問的情況。...

基於SpringBoot 2 X整合Druid

說明 本文旨在整理springboot 2.x整合druid基礎功能,如有問題請指出 參考資料 基於springboot 2.x版本,這裡是引入druid spring boot2 starter的方式來配置,所以不需要不需要configbean來配置druid,如果引入的是druid,則需要con...

SpringBoot2 x 整合Druid詳細步驟

druid spring boot starter模式 這種方式比較簡單,不需要自己寫配置類。在匯入依賴以及配置好配置檔案yml即可。pom.xml com.alibaba druid spring boot starter 1.1.10 spring datasource url jdbc mys...