SpringBoot在項(xiàng)目中訪問靜態(tài)資源步驟分析
在springboot項(xiàng)目中如果要在不集成templates的情況下訪問靜態(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來自與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;
所以要訪問靜態(tài)資源需要配置到這四個(gè)路徑下,如果所示
3.API端按如下編碼
@Controller public class PageApi { @GetMapping({"/", "/index"}) public String toPage(String id){ return "index"; } }
總結(jié):
按照上面的配置后我們就可以訪問到靜態(tài)資源。
到此這篇關(guān)于SpringBoot在項(xiàng)目中訪問靜態(tài)資源步驟分析的文章就介紹到這了,更多相關(guān)SpringBoot訪問靜態(tài)資源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java微信公眾平臺(tái)開發(fā)(2) 微信服務(wù)器post消息體的接收
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第二步,微信服務(wù)器post消息體的接收,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04java解析xml的4種方式的優(yōu)缺點(diǎn)對(duì)比及實(shí)現(xiàn)詳解
這篇文章主要介紹了java解析xml的4種方式的優(yōu)缺點(diǎn)對(duì)比及實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07WebUploader+SpringMVC實(shí)現(xiàn)文件上傳功能
WebUploader是由Baidu團(tuán)隊(duì)開發(fā)的一個(gè)簡(jiǎn)單的以HTML5為主,F(xiàn)LASH為輔的現(xiàn)代文件上傳組件。這篇文章主要介紹了WebUploader+SpringMVC實(shí)現(xiàn)文件上傳功能,需要的朋友可以參考下2017-06-06Java中消息隊(duì)列任務(wù)的平滑關(guān)閉詳解
對(duì)于消息隊(duì)列的監(jiān)聽,我們一般使用Java寫一個(gè)獨(dú)立的程序,在Linux服務(wù)器上運(yùn)行。程序啟動(dòng)后,通過消息隊(duì)列客戶端接收消息,放入一個(gè)線程池進(jìn)行異步處理,并發(fā)的快速處理。這篇文章主要給大家介紹了關(guān)于Java中消息隊(duì)列任務(wù)的平滑關(guān)閉的相關(guān)資料,需要的朋友可以參考下。2017-11-11SpringCloud HystrixDashboard服務(wù)監(jiān)控詳解
Hystrix Dashboard 是Spring Cloud中查看Hystrix實(shí)例執(zhí)行情況的一種儀表盤組件,支持查看單個(gè)實(shí)例和查看集群實(shí)例,本文將對(duì)其服務(wù)監(jiān)控學(xué)習(xí)2022-11-11如何在springboot項(xiàng)目中自定義404頁面
今天點(diǎn)擊菜單的時(shí)候不小心點(diǎn)開了一個(gè)不存在的頁面,然后看到瀏覽器給的一個(gè)默認(rèn)的404頁面,這篇文章主要介紹了如何在springboot項(xiàng)目中自定義404頁面,需要的朋友可以參考下2024-05-05Springboot整合MongoDB的Docker開發(fā)教程全解
這篇文章主要介紹了Springboot整合MongoDB的Docker開發(fā),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2020-07-07