靜態資源訪問配置
https://www.jianshu.com/p/b6e0a0df32ec
https://segmentfault.com/q/1010000012240531/a-1020000012250927
_______________________________________________________________________________________________
springboot-靜態資源默認訪問路徑順序
META-INF/resources/hello.html hello.html內容META-INF/resources/hello
static/hello.html hello.html內容static/hello
resources/hello.html 內容resources/hello
public/hello.html 內容public/hello
訪問http://localhost/hello.html
頁面內容META-INF/resources/hello,訪問路徑是META-INF/resources/hello.html,去掉該html
再訪問http://localhost/hello.html
內容:resources/hello,訪問路徑是resources/hello.html去掉該html
訪問http://localhost/hello.html
內容:static/hello,訪問路徑static/hello.html,去掉該html
再訪問http://localhost/hello.html
內容resources/public/hello world,訪問路徑public/hello world,去掉該html
由此實驗得知靜態資源默認訪問路徑是
META-INF/resources > resources > static > public
________________________________________________________________________________________________
Springboot 之 靜態資源路徑配置
1、靜態資源路徑是指系統可以直接訪問的路徑,且路徑下的所有文件均可被用戶通過瀏覽器直接讀取。
2、在Springboot中默認的靜態資源路徑有:classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
3、在Springboot中可以直接在配置文件中覆蓋默認的靜態資源路徑的配置信息:
#自定義的屬性,指定了一個路徑,注意要以/結尾
web.upload-path=D:/temp/study13/
#表示所有的訪問都經過靜態資源路徑 spring.mvc.static-path-pattern=/**
#覆蓋默認配置,所以需要將默認的也加上否則static、public等這些路徑將不能被當作靜態資源路徑
#在最末尾的file:${web.upload-path}中的file:表示是一個具體的硬盤路徑,其他的使用classpath指的是系統環境變量
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
4、在SpringBoot開發中,可以在Java代碼中覆蓋默認靜態資源配置
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if(!registry.hasMappingForPattern("/static/**")){ registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } super.addResourceHandlers(registry); } }
5、由於Spring Boot 默認資源路徑配置的問題,使用IDEA開發Spring Boot應用時,會導致一個問題————瀏覽器、編輯器 不能同時訪問 JS 等資源的問題。這時往往通過配置 4 中的代碼,來實現同時訪問資源文件的效果
參考知識林:http://www.zslin.com/web/article/detail/23
_______________________________________________________________________________________________
首先我用create-react-app搭建了一個react項目(這一步操作大家可以去官網看https://reactjs.org/docs/add-react-to-a-new-app.html)
你會得到一個結構如下的項目:

我們可以通過 yarn start 運行這個項目:


跑起來的頁面是這樣的:

好的,現在你已經成功在開發環境中跑起來了,接下來我們來打包,然后將其部署到服務器上
打包也很簡單,執行 npm run build :

你會發現在你的項目文件夾里多了個build文件夾:


然后當你點擊index.html之后,會發現打開是這樣的:

一片空白...然后你檢查了了下index.html,發現里面的路徑是這樣的:

發現了啥問題沒...里面的路徑是絕對路徑,所以當然找不到js和css以及圖片資源啥的,那怎么讓這些路徑變成相對路徑呢,很簡單...我們再package.json加上homepage就行:

大家看最后一句就行...然后我們再次打包,然后再點index.html,會發現一切正常:

好的,現在我們通過build得到了html頁面以及js和css和各種資源...你也發現了,我們網頁的入口是index.html
所以比如說你自己有個服務器地址是 www.abc.com ,你只要在訪問www.abc.com的時候把index.html返回出去就行了...因為我自己的服務器是用SpringBoot搭建的,所以我就用SpringBoot來舉例子
SpringBoot返回html頁面也很簡單,在resource目錄下新建一個public文件夾,然后把你React打包的build文件夾里的文件都丟進去就行...(這里截圖是我自己的項目,我把一些.js和.json文件去掉了,因為好像沒啥用)

這個時候你訪問www.abc.com他就會直接返回index.html了(注意在SpringBoot里的Controller去掉對"/"的攔截)
然后你只要把SpringBoot項目部署到服務器上(如何部署SpringBoot項目大家可以看這篇文章https://blog.csdn.net/ruglcc/article/details/76147645),然后訪問你的域名,你就可以看到index.html了,比如我剛剛部署的自己的網頁www.nanmian.top
OK這篇文章結束了,大家也發現了目前的網頁很簡單...就只有一個頁面,我剛學前端...所以也不是很懂,不知道之后項目變大了這種方法還行不行...到時候我會再記錄的
______________________________________________________________________________