静的リソースのマッピング

3455 ワード

Spring BootとWeb開発-静的リソースのマッピング
Spring Bootの使用:
  • SpringBootを作成し、必要なモジュール
  • を選択します.
  • SpringBootのデフォルト設定シーンは、プロファイルに少量の構成を指定するだけで
  • です.
  • 業務コード作成
  • 静的リソースのマッピング規則
    @ConfigurationProperties(
        prefix = "spring.resources",
        ignoreUnknownFields = false
    )
    public class ResourceProperties {
        //           ,   
    
    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(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                    }
    
                }
            }
    
    
    //      
     @Bean
     public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
                WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
                welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
                welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
                return welcomePageHandlerMapping;
            }
    
    
  • すべての/webjars/**の要求は、classpath:/META-INF/resources/webjars/リソースを探します.webjars:jarパケット方式で静的リソースを導入する;​ eg: META-INF\resources\webjars\jquery\3.3.1\jquery.js送信localhost:8080/webjars/jquery/3.3.1/jquery.jsは、依存
    
            
                org.webjars
                jquery
                3.3.1
            
    
  • を導入するだけでアクセスできる.
  • "/**は、現在のプロジェクトのリソース
    "classpath:/META-INF/resources/"
    "classpath:/resources/"
    "classpath:/static/"
    "classpath:/public/"
    "/"   //        
    
  • にアクセスします.
  • classpath:クラスパスの下のディレクトリ:javaとresourceの下の
  • トップページ;静的リソースフォルダの下にあるすべてのindex.htmlページは「/**」にマッピングされます.localhost:8080/indexページ
  • すべての**/favicon.icoは静的リソースファイルの下で探しています.画像をfaviconに変更します.icoは、過去を自動的にマッピングすることができ、トップページアイコン
  • カスタム静的リソースフォルダパス:
    spring.resources.static-locations=classpath:/hello/