将jquery.js和index.html放在src/main/resource源目录下
输入地址 ,发现找不到资源
http://localhost:8088/jquery.js
1.默认地址
查看源码WebMvcAutoConfiguration.java中的WebMvcAutoConfigurationAdapter静态内部类WebMvcAutoConfigurationAdapter中的addResourceHandlers方法
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache()
.getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(
this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl));
}
}
跟踪this.resourceProperties.getStaticLocations()可以看到默认查找的地址
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
将资源放在这四个位置,springboot可以找到.
优先级顺序为:META-INF/resources > resources > static > public
2.继承WebMvcConfigurerAdapter或者实现WebMvcConfigurer
package com.spring.boot.config.web;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableWebMvc
@Configuration
public class mvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将所有/** 访问都映射到classpath:/ 目录下
//也可配置/static/** 访问都映射到classpath:/static/ 目录下
registry.addResourceHandler("/**").addResourceLocations("classpath:/");
}
}
如果非必要的情况不建议这样配置,因为这样配置springboot的web自动配置将不生效,其它项需要自己配置
3.application.properties配置
spring.resources.static-locations=classpath:/
可以配合spring.mvc.static-path-pattern使用
spring.resources.static-locations=classpath:/
spring.mvc.static-path-pattern=/static/**
访问路径就变为
http://localhost:8080/static/jquery.js
4.maven编译打包
在pom.xml中配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--targetPath将打包到指定的目录-->
<targetPath>META-INF/resources</targetPath>
<!--表示index.html, jquery.js是一个资源-->
<includes>
<include>index.html</include>
<include>jquery.js</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!--排除index.html, jquery.js-->
<excludes>
<exclude>index.html</exclude>
<exclude>jquery.js</exclude>
</excludes>
<includes>
<include>**</include>
</includes>
</resource>
</resources>
</build>
查看编译后的格式