欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在SpringBoot中靜態(tài)資源訪問方法

 更新時(shí)間:2017年11月30日 16:23:07   作者:王學(xué)政  
這篇文章給大家介紹了在SpringBoot中靜態(tài)資源訪問方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

一、概述

springboot 默認(rèn)靜態(tài)資源訪問的路徑為:/static 或 /public 或 /resources 或 /META-INF/resources 這樣的地址都必須定義在src/main/resources目錄文件中,這樣可以達(dá)到在項(xiàng)目啟動時(shí)候可以自動加載為項(xiàng)目靜態(tài)地址目錄到classpath下 ,靜態(tài)訪問地址其實(shí)是使用 ResourceHttpRequestHandler 核心處理器加載到WebMvcConfigurerAdapter進(jìn)行對addResourceHandlers方法進(jìn)行覆蓋.將靜態(tài)訪問目錄進(jìn)行重新定義。我們也可以實(shí)現(xiàn)其中方法,手動指定靜態(tài)訪問路徑通過繼承WebMvcConfigurerAdapter重寫內(nèi)部方法addResourceHandlers也可以達(dá)到我們想要的效果。

第一種方式 : 放在src/main/webapp目錄下

放在webapp目錄下的靜態(tài)資源是可以直接訪問的

這里寫圖片描述

user.html

這里寫圖片描述

2.png

這里寫圖片描述

在user.html中引用2.png

這里寫圖片描述

第二種方式:放在classpath下

ResourceProperties中的說明

org.springframework.boot.autoconfigure.web.ResourceProperties
 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
   "classpath:/META-INF/resources/", "classpath:/resources/",
   "classpath:/static/", "classpath:/public/" };

靜態(tài)資源默認(rèn)放在classpath路徑下:Defaults to classpath:[/META-INF/resources/,/resources/, /static/, /public/] plus context:/ (the root of the servlet context).

這里寫圖片描述

person/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link href="/css/main.css" rel="external nofollow" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/main.js"></script>
<script type="text/javascript">
 sayHello();
</script>
</head>
<body>
 <h3>person page HTML</h3>
</body>
</html>

這里寫圖片描述

通過修改配置項(xiàng),設(shè)置靜態(tài)資源的位置

application.properties
# 修改默認(rèn)的靜態(tài)資源存放目錄
spring.resources.static-locations=classpath:/web/

這里寫圖片描述

總結(jié)

以上所述是小編給大家介紹的在SpringBoot中靜態(tài)資源訪問方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論