系列传送门
spring-boot与模板引擎:静态资源文件路径
spring-boot与模板引擎:修改模板路径和后缀名
spring-boot与模板引擎:使用metisMenu实现动态多级菜单
在spring-boot与模板引擎:修改模板路径和后缀名一文中,我们分析了下模板引擎的路径和后缀名设置,下面让我们继续分析spring-boot对静态资源文件的读取。
默认读取的静态资源路径
查看包名org.springframework.boot.autoconfigure.web下的ResourceProperties源码:
public class ResourceProperties implements ResourceLoaderAware {
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private static final String[] RESOURCE_LOCATIONS;
......
......
}
可以看到,spring-boot默认读取的资源资源文件根路径有4个:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
在启动spring-boot项目的时候,我们可以看到下面日志:
[ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
其中 [/webjars/**] ,映射的目录为 classpath:/META-INF/resources/webjars/。
其中 [/**] ,映射的目录为 classpath:/resources/,classpath:/static/和classpath:/public/。
其中 [/**/favicon.ico] ,映射的路径为 classpath:/resources/favicon.ico,或classpath:/static/favicon.ico,或classpath:/public/favicon.ico。
需要注意的是,如果是用Intellij IDEA开发,项目默认生成的Resources目录,不是上面说的“classpath:/resources/”,这个Resources目录是直接指向“classpath:/”的。Resources目录下新建一个“resources”文件夹,此时“resources”文件夹的路径才是“classpath:/resources/”。
下面我们看个例子:
那么,网页中,我们应该怎么写,才能访问到bootstrap.min.js这个静态资源呢?
答案如下:
<script src="/js/bootstrap.min.js"></script>
手动配置静态资源路径
spring的包中提供了一个抽象类:WebMvcConfigurerAdapter:
......
......
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
......
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
......
}
......
......
我们可以自己创建一个类,继承该类,并重写addResourceHandlers方法:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
// 将META-INF/resources/test目录下的所有文件,映射到classpath:/static/路径下
registry.addResourceHandler("/test/**").addResourceLocations("classpath:/static/");
}
}
除了上面的方式外,也可以通过配置Application.properties来设置:
# 默认为/**,此时在html里面访问classpath:/static/js/test.js,需要这样写:/js/xxx.js
# 配置为/static/**,此时在html里面访问classpath:/static/js/test.js,需要这样写:/static/js/xxx.js
spring.mvc.static-path-pattern=/**
# 默认为classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
# 手动配置多个,需要用逗号分隔,比如classpath:/js/,classpath:/css/,此时在html里面访问classpath:/js/test.js,需要这样写:/test.js
spring.resources.static-locations=classpath:/js/,classpath:/css/