静态资源路径
类路径下:/resources/static/、/resources/public/、/resources/resources/、/resources/META-INF/resources 这些路径下的资源均可直接访问;通过 http://ip:port/资源名称 访问即可
可在配置文件中对访问路径和访问拦截规则进行设置,如下是 SpringBoot 默认的配置和开发过程中常用配置
#可直接访问的静态资源路径
在application.properties或application.yml文件中添加spring.resources.static-locations属性,指定静态资源文件的路径。
多个路径可以使用逗号分隔。 例如,在application.yml中配置:
spring:
resources:
static-locations:
- classpath:/custom-static/
- classpath:/static/
这样配置后,Spring Boot会按照指定的顺序去查找静态资源文件,先从classpath:/custom-static/目录中查找,
如果找不到再从classpath:/static/目录中查找。
另一种方式是通过编写Java配置类来进行配置。创建一个继承自WebMvcConfigurer的配置类,并重写addResourceHandlers方法。
例如,创建一个名为WebConfig的配置类:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/custom/**")
.addResourceLocations("classpath:/custom-static/");
}
}
这样配置后,所有以/custom/开头的URL路径都会被映射到classpath:/custom-static/目录下的静态资源文件。
无论使用哪种方式进行配置,配置完成后,你就可以根据指定的静态资源路径来访问对应的资源文件了。
例如,如果静态资源文件夹是/static,
则可以通过http://localhost:8080/static/example.jpg
来访问名为example.jpg的图片文件。
#访问拦截规则
spring.mvc.static-path-pattern = /**
#静态资源缓存时间(秒)
spring.resources.cache.period = 10
原理:静态资源映射路径(/**)默认拦截所有请求路径,但接收到的请求会先匹配 controller,如果 controller 未能匹配再交给静态资源处理器处理
静态资源访问前缀
如果需要修改静态资源访问路径的前缀,可在配置文件中添加如下配置即可
#通过 http://localhost:8080/assets/xxx 路径访问
spring.mvc.static-path-pattern = /assets/**
默认首页
SpringBoot 可直接访问静态资源路径下的 index.html 页面而不需要在路径中添加 index.html;例如:我们把 index.html 放到 /resources/static/ 目录路径下,通过 http://localhost:8080 就可以访问
当浏览器请求静态资源时,Spring Boot使用ResourceHttpRequestHandler
类来处理这些请求。我们来看一下它的源码,以便更好地理解其原理和实现方式。 ResourceHttpRequestHandler
类是Spring MVC框架中用于处理静态资源请求的处理器。它继承自AbstractHttpRequestHandler
类,并实现了HttpRequestHandler
接口。 下面是ResourceHttpRequestHandler
类的简化版本的源码:
public class ResourceHttpRequestHandler extends AbstractHttpRequestHandler {
private List<Resource> locations;
private List<UrlPathHelper> urlPathHelpers = new ArrayList<>();
@Override
protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
String requestPath = getRequestPath(request);
Resource resource = getResource(requestPath);
if (resource != null && resource.exists()) {
// 返回静态资源文件
response.setContentType(getMimeType(resource));
response.setContentLengthLong(resource.contentLength());
FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
} else {
// 返回404错误
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
private Resource getResource(String requestPath) {
for (Resource location : this.locations) {
Resource resource = location.createRelative(requestPath);
if (resource.exists() && resource.isReadable()) {
return resource;
}
}
return null;
}
// 其他辅助方法...
}
从上述源码可以看出,ResourceHttpRequestHandler
类的主要逻辑是在handleRequest
方法中处理请求。它首先获取请求的URL路径,然后根据URL路径查找对应的资源文件。
如果找到了资源文件并且存在,就将其返回给浏览器;否则,返回404错误。 资源文件的查找是通过遍历locations
列表中的路径,并调createRelative
方法来创建对应的资源对象。
如果资源文件存在且可读,则返回该资源文件。 需要注意的是,locations
列表是通过配置文件中的spring.resources.static-locations
属性来配置的,它指定了静态资源文件的路径。
通过以上分析,我们可以了解到Spring Boot静态资源处理的实现方式是通过ResourceHttpRequestHandler
类来处理请求,根据请求的URL路径查找对应的资源文件,并将其返回给浏览器。这样可以方便地管理和访问静态资源文件,提供了便捷的开发体验。
小露:这个原理听起来有些枯燥!你能给我一个具体的实例演示吗?
小朝:当然可以!假设我们有一个名为index.html
的静态资源文件,放在/static
目录下。客户端请求/index.html
路径,Spring Boot会自动找到该资源文件,并将其返回给客户端。
小露:那么,我们只需要在浏览器中访问http://localhost:8080/index.html
就可以了?
小朝:是的,你可以通过这个URL来访问index.html
文件,Spring Boot会自动处理并将该文件返回给你。