静态资源访问static-locations和static-path-pattern

静态资源配置底层源码:

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (!this.resourceProperties.isAddMappings()) {
			logger.debug("Default resource handling disabled");
				return;
		}
        //配置访问地址为/webjars/**时,去/META-INF/resources/webjars文件夹下寻找资源
		addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
		addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
				            
        registration.addResourceLocations(this.resourceProperties.getStaticLocations());
		if (this.servletContext != null) {
				ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
				registration.addResourceLocations(resource);
		}
			});
		}

静态资源默认前缀:

private String staticPathPattern = "/**";

静态资源默认地址:

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;

 静态资源目录 

如果每个目录下面都有相同的文件,那么访问的优先级为  META-INF>resources>static>public 

静态资源访问前缀(默认无前缀)可以使用下面的yaml内容来设置

spring:
  mvc:
    static-path-pattern: /liang/**  //会导致欢迎页和favicon.ico失效

静态资源存放地址(静态文件只能存放在文件夹yuan里面)

spring:
  web:
    resources:
      static-locations: classpath:/yuan/

当配置文件如下

spring
  web:
    resources:
      static-locations: classpath:/yuan/
  mvc:
    static-path-pattern: /liang/**

可以直接通过地址 http://localhost:8080/liang/a.png 直接进行访问,查看到想要结果

当静态访问前缀为/**时,静态资源目录下有一个a.png,controller控制层的@RequestMapping("/a.png")。

 得到结果

 结论:请求进来,先去controller看能不能处理,不能处理的所有请求又都交给静态资源处理器。静态资源找不到就报404

为什么欢迎页(index.html)有静态资源访问前缀就不能访问了

 通过 http://localhost:8080/liang/index.html可以直接访问到界面,但是通过 http://localhost:8080/liang 或者 http://localhost:8080/ 都不能直接访问到index。

但是如果把静态资源访问前缀去除,就可以通过 http://localhost:8080/ 访问到index.html了.

这是因为底层做了处理

 

实现WebMvcConfigurer接口,会把自定义配置加载到默认的配置中

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("访问的路径").addResourceLocations("资源的路径");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    }

配置文件中静态资源目录为

 可以简单理解为:实现WebMvcConfigurer接口,可以把自己自定义的一些配置加载到系统的默认配置中

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot项目中,可以使用`spring.resources.static-locations`属性来指定查找本地静态文件的位置。该属性可以在项目的配置文件(如application.properties或application.yml)中进行配置。通过配置该属性,可以告诉Spring Boot在哪些目录下查找静态文件。 例如,如果想要在项目的根目录下的static文件夹和public文件夹中查找静态文件,可以在配置文件中添加以下配置: ``` spring.resources.static-locations=classpath:/static/,classpath:/public/ ``` 这样,当访问静态资源时,Spring Boot会在这两个目录下查找对应的文件。如果找到匹配的文件,就会返回给客户端。 需要注意的是,`spring.resources.static-locations`属性的值可以是一个或多个目录路径,多个路径之间使用逗号进行分隔。路径可以是相对路径或绝对路径,也可以是classpath路径。 希望这个解答对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [spring.mvc.static-path-pattern和spring.resources.static-locations](https://blog.csdn.net/baidu_38225647/article/details/109464223)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Spring Boot配置静态资源的地址与访问路径(spring.mvc.static-path-pattern和spring.web.resources.static-...](https://blog.csdn.net/weixin_38924500/article/details/109739021)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值