public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware { private final ResourceProperties resourceProperties; //1-配置靜態訪問資源
public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); } else { Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl(); if (!registry.hasMappingForPattern("/webjars/**")) { this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}) .addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}) .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); //靜態資源文件夾映射
if (!registry.hasMappingForPattern(staticPathPattern)) { this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } } } }
1-2-1-去webjars官網獲取本身須要的maven依賴html
1-2-2-將maven依賴導入行pom.xml文件java
1-2-3-啓動項目,測試是否能夠獲取jquery資源(示例路徑:http://localhost:8080/webjars/jquery/3.3.1/jquery.js)react
//ResourceProperties類
@ConfigurationProperties( prefix = "spring.resources",//若是要本身配置路徑,在配資前綴爲"spring.resources"
ignoreUnknownFields = false ) public class ResourceProperties { //聲明瞭默認classpath資源路徑爲:{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}; }
#在application.properities中進行配置,多個路徑能夠用逗號隔開 spring.resources.static-locations=classpath:/myresources/
2-5-1-重寫WebMvcConfigurerAdapter 中的addResourceHandlers方法,自定義映射路徑jquery
@Configuration public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter { /** * 配置靜態訪問資源 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/mypath/**").addResourceLocations("classpath:/myresources/"); super.addResourceHandlers(registry); } }
2-5-2-應用示例web
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware { private final ResourceProperties resourceProperties; //1-指定this.getWelcomePage(),見2
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) { return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern()); } //2-去getIndexHtml()獲取歡迎頁面路徑,見3
private Optional<Resource> getWelcomePage() { String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations()); return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst(); } //3-默認歡迎頁面在location(映射路徑) + "index.html"
private Resource getIndexHtml(String location) { return this.resourceLoader.getResource(location + "index.html"); } }
參看資料:spring
1-https://blog.csdn.net/baidu_36216018/article/details/79699084瀏覽器
2-https://www.cnblogs.com/java-synchronized/p/7091723.htmlmvc
3-https://blog.csdn.net/javareact/article/details/77981769app