zoukankan      html  css  js  c++  java
  • SpringBoot对静态资源配置

    SpringBoot中SpringMVC的相关配置都在WebMvcAuotConfiguration这个类中

    1 addResourceHandlers:添加资源映射

    /**
      1 所有/webjars/**,都去classpath:/META-INF/resources/webjars/ 找资源;webjars:以jar包的 
        方式引入静态资源;网址http://www.webjars.org。如:
        <dependency>
    	  <groupId>org.webjars</groupId>
    	  <artifactId>jquery</artifactId>
    	  <version>3.3.1-1</version>
        </dependency>
        访问:localhost:8080/webjars/jquery/3.3.1-1/jquery.js
      2 "/**"访问当前项目的任何资源,都去(静态资源的文件夹)找映射,默认的静态资源文件夹:
        "classpath:/META‐INF/resources/",
        "classpath:/resources/",
        "classpath:/static/",
        "classpath:/public/"
        "/":当前项目的根路径
    */
    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));
    		}
    
    	}
    }
    
    //可以自定义静态资源文件路径
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,
      classpath:/static/,classpath:/public/,D:/files

    2 配置欢迎页

    //遍历静态资源文件夹下的所有index.html页面;然后被"/**"映射;
    @Bean
    public WebMvcAutoConfiguration.WelcomePageHandlerMapping welcomePageHandlerMapping(ResourceProperties resourceProperties) {
      return new WebMvcAutoConfiguration.WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),this.mvcProperties.getStaticPathPattern());
    }
    
    private String[] getStaticWelcomePageLocations() {
    	String[] result = new String[this.staticLocations.length];
    
    	for(int i = 0; i < result.length; ++i) {
    		String location = this.staticLocations[i];
    		if (!location.endsWith("/")) {
    			location = location + "/";
    		}
    
    		result[i] = location + "index.html";
    	}
    
    	return result;
    }
    

    3 设置图标

    //所有的**/favicon.ico 路径下图标,都是在静态资源文件下找资源
    public static class FaviconConfiguration {
    	private final ResourceProperties resourceProperties;
    
    	public FaviconConfiguration(ResourceProperties resourceProperties) {
    		this.resourceProperties = resourceProperties;
    	}
    
    	@Bean
    	public SimpleUrlHandlerMapping faviconHandlerMapping() {
    		SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    		mapping.setOrder(-2147483647);
    		mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
    		return mapping;
    	}
    
    	@Bean
    	public ResourceHttpRequestHandler faviconRequestHandler() {
    		ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
    		requestHandler.setLocations(this.resourceProperties.getFaviconLocations());
    		return requestHandler;
    	}
    }

    4 可以设置和静态资源有关的参数,缓存时间等

    @ConfigurationProperties(prefix="spring.resources", ignoreUnknownFields=false)
    public class ResourceProperties implements ResourceLoaderAware{
    只有把命运掌握在自己手中,从今天起开始努力,即使暂时看不到希望,也要相信自己。因为比你牛几倍的人,依然在努力。
  • 相关阅读:
    《程序员代码面试指南》第八章 数组和矩阵问题 最长的可整合子数组的长度...
    Vue项目中jQuery的引入
    阿里云ECS安装JAVA+MYSQL+NGINX
    WCF寄宿与IIS里时遇到的问题
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/freesky168/p/14358238.html
Copyright © 2011-2022 走看看