SpringBoot在項(xiàng)目中訪問(wèn)靜態(tài)資源步驟分析
在springboot項(xiàng)目中如果要在不集成templates的情況下訪問(wèn)靜態(tài)資源需要做以下配置
1.在項(xiàng)目的application.yml文件中做如下配置
spring:
profiles:
active: dev
mvc:
view:
prefix: /
suffix: .html
重點(diǎn)在

配置后生成為WebMvcProperties 配置類。該配置類中有一個(gè)內(nèi)部類View
@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {View類可以配置視圖的前綴和后綴
public static class View {
/**
* Spring MVC view prefix. 前綴
*/
private String prefix;
/**
* Spring MVC view suffix. 后綴
*/
private String suffix;2.在項(xiàng)目的resource路徑下新建文件夾
在ResourceHttpRequestHandler類的getResource方法中調(diào)用了getLocations()方法。
protected Resource getResource(HttpServletRequest request) throws IOException {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
if (path == null) {
throw new IllegalStateException("Required request attribute '" +
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE + "' is not set");
}
path = processPath(path);
if (!StringUtils.hasText(path) || isInvalidPath(path)) {
if (logger.isTraceEnabled()) {
logger.trace("Ignoring invalid resource path [" + path + "]");
}
return null;
}
if (isInvalidEncodedPath(path)) {
if (logger.isTraceEnabled()) {
logger.trace("Ignoring invalid resource path with escape sequences [" + path + "]");
}
return null;
}
ResourceResolverChain resolveChain = new DefaultResourceResolverChain(getResourceResolvers());
//重點(diǎn)關(guān)注此處
Resource resource = resolveChain.resolveResource(request, path, getLocations());
if (resource == null || getResourceTransformers().isEmpty()) {
return resource;
}
ResourceTransformerChain transformChain =
new DefaultResourceTransformerChain(resolveChain, getResourceTransformers());
resource = transformChain.transform(request, resource);
return resource;
}getLocations()方法返回的locations來(lái)自與springboot項(xiàng)目,其中時(shí)配置類ResourceProperties賦值。賦值的數(shù)據(jù)為
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
四個(gè)路徑
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;所以要訪問(wèn)靜態(tài)資源需要配置到這四個(gè)路徑下,如果所示

3.API端按如下編碼
@Controller
public class PageApi {
@GetMapping({"/", "/index"})
public String toPage(String id){
return "index";
}
}總結(jié):
按照上面的配置后我們就可以訪問(wèn)到靜態(tài)資源。

到此這篇關(guān)于SpringBoot在項(xiàng)目中訪問(wèn)靜態(tài)資源步驟分析的文章就介紹到這了,更多相關(guān)SpringBoot訪問(wèn)靜態(tài)資源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Springboot啟動(dòng)報(bào)錯(cuò):類文件具有錯(cuò)誤的版本61.0,應(yīng)為?52.0
這篇文章主要給大家介紹了關(guān)于解決Springboot啟動(dòng)報(bào)錯(cuò):類文件具有錯(cuò)誤的版本?61.0,應(yīng)為?52.0的相關(guān)資料,這是查閱了網(wǎng)上的很多資料才解決的,分享給大家,需要的朋友可以參考下2023-01-01
SpringBoot安全認(rèn)證Security的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot安全認(rèn)證Security的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
springBoot Maven 剔除無(wú)用的jar引用問(wèn)題記錄
這篇文章主要介紹了springBoot Maven 剔除無(wú)用的jar引用問(wèn)題記錄,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-12-12
SpringMVC通過(guò)Ajax處理Json數(shù)據(jù)的步驟詳解
這篇文章主要介紹了SpringMVC通過(guò)Ajax處理Json數(shù)據(jù)的步驟詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Java讀取文件的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Java讀取文件的簡(jiǎn)單實(shí)現(xiàn)方法,通過(guò)一個(gè)讀取txt格式的log文件為例,詳細(xì)的講述了Java讀取文件的方法及原理,需要的朋友可以參考下2014-09-09
kafka分布式消息系統(tǒng)基本架構(gòu)及功能詳解
這篇文章主要為大家介紹了kafka分布式消息系統(tǒng)基本架構(gòu)及功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
springboot加載命令行參數(shù)ApplicationArguments的實(shí)現(xiàn)
本文主要介紹了springboot加載命令行參數(shù)ApplicationArguments的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

