最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot在項目中訪問靜態(tài)資源步驟分析

 更新時間:2023年01月28日 10:11:30   作者:xl649138628  
今天在玩SpringBoot的demo的時候,放了張圖片在resources目錄下,啟動區(qū)訪問的時候,突然好奇是識別哪些文件夾來展示靜態(tài)資源的, 為什么有時候放的文件夾不能顯示,有的卻可以

在springboot項目中如果要在不集成templates的情況下訪問靜態(tài)資源需要做以下配置

1.在項目的application.yml文件中做如下配置

spring:
  profiles:
    active: dev
  mvc:
    view:
      prefix: /
      suffix: .html

重點在

配置后生成為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());
        //重點關(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)文章

最新評論

曲麻莱县| 兴和县| 海原县| 新郑市| 韶关市| 冀州市| 兴城市| 潜山县| 东丰县| 定边县| 昌乐县| 汽车| 利津县| 出国| 宜黄县| 祁阳县| 漾濞| 体育| 布尔津县| 龙川县| 和林格尔县| 新晃| 增城市| 彭阳县| 桑日县| 牡丹江市| 商水县| 肃北| 益阳市| 扶沟县| 沽源县| 靖西县| 始兴县| 宣汉县| 建平县| 拉萨市| 利辛县| 皮山县| 汾西县| 兴文县| 陈巴尔虎旗|