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

配置后生成為WebMvcProperties 配置類。該配置類中有一個內(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.在項目的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項目,其中時配置類ResourceProperties賦值。賦值的數(shù)據(jù)為
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
四個路徑
@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)資源需要配置到這四個路徑下,如果所示

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

到此這篇關(guān)于SpringBoot在項目中訪問靜態(tài)資源步驟分析的文章就介紹到這了,更多相關(guān)SpringBoot訪問靜態(tài)資源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java微信公眾平臺開發(fā)(2) 微信服務(wù)器post消息體的接收
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第二步,微信服務(wù)器post消息體的接收,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
java解析xml的4種方式的優(yōu)缺點(diǎn)對比及實(shí)現(xiàn)詳解
這篇文章主要介紹了java解析xml的4種方式的優(yōu)缺點(diǎn)對比及實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
WebUploader+SpringMVC實(shí)現(xiàn)文件上傳功能
WebUploader是由Baidu團(tuán)隊開發(fā)的一個簡單的以HTML5為主,F(xiàn)LASH為輔的現(xiàn)代文件上傳組件。這篇文章主要介紹了WebUploader+SpringMVC實(shí)現(xiàn)文件上傳功能,需要的朋友可以參考下2017-06-06
SpringCloud HystrixDashboard服務(wù)監(jiān)控詳解
Hystrix Dashboard 是Spring Cloud中查看Hystrix實(shí)例執(zhí)行情況的一種儀表盤組件,支持查看單個實(shí)例和查看集群實(shí)例,本文將對其服務(wù)監(jiān)控學(xué)習(xí)2022-11-11
Springboot整合MongoDB的Docker開發(fā)教程全解
這篇文章主要介紹了Springboot整合MongoDB的Docker開發(fā),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值 ,需要的朋友可以參考下2020-07-07

