(6)Spring BootとWeb開発

前提:    Spring Boot 2.2.5およびWeb開発の公式Webサイトのドキュメント                                                                            

Spring Bootの3つの軸をうまく活用してください。

  •  Spring Bootアプリケーションを作成し、必要なモジュールを選択します。
  • Spring Bootはデフォルトでこれらのシナリオを構成しており、実行するには構成ファイルで少量の構成を指定するだけで済みます。
  • 自分でビジネスコードを記述します。  

   公式ウェブサイトディレクトリリファレンス:

                                                 

まず、自動構成の原則は?

  このシナリオでは、Spring Bootは何を構成しますか?変更できますか?どの構成を変更できますか?拡張できますか?XXX

     

xxxxAutoConfiguration:帮我们给容器中自动配置组件;
xxxxProperties:配置类来封装配置文件的内容;

第二に、静的リソースに対するSpring Bootのマッピングルール

静的リソースに関連するパラメーターを設定する;   WebMvcAutoConfiguration

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]
                         {"classpath:/META-INF/resources/",
                          "classpath:/resources/", 
                          "classpath:/static/",
                          "classpath:/public/"};
    private String[] staticLocations;

}


// 添加静态 资源文件路径配置
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));
            return welcomePageHandlerMapping;
        }

(1)すべての  「/ webjars / **」   、「classpath:/ META-INF / resources / webjars /」に移動してリソースを検索します

         -webjars:公式Webサイトのリソース  にjarパッケージの形式で静的リソースを導入します               

        <!--引入 Jquery 的webjars  在访问的时候只需要写webjars下面资源的名称即可-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.1</version>
        </dependency>

 

               

(2)staticPathPattern = "/ **"     現在のプロジェクトのリソースアクセスするには[静的リソースのフォルダー]

  •  「クラスパス:/ META-INF / resources /」
  •  「クラスパス:/リソース/」
  •  "クラスパス:/静的/"、
  • 「classpath:/ public /」
  • "/":現在のプロジェクトの変更パス

結論:アクセス方法は次のとおりです:localhost:8080 /フォルダー名/ ***    静的リソースフォルダーに移動し、フォルダーパスの下で対応する静的リソースを見つけます

(3)ウェルカムページ:静的リソースフォルダー内のすべてのindex.htmlページ、「/ **」でマッピング

      したがって、ウェルカムページは、localhost:8080 /でindex.htmlを見つけることです。  

(4)favicon.icoアイコン設定   リンクの分析springboot2.2.5 favicon.icoアイコンを削除する理由

        このSpring Bootプロジェクトもサポートを提供しますが、バージョンが異なります。インターネット上の記事のほとんどは最新バージョンでは無効になっています。この記事では、Spring Boot 2.xバージョンでの使用方法をすべての人に紹介しています。

異なるバージョンのSpring Bootがファビコンをサポート

以前のバージョンでは、Spring Bootはデフォルトでファビコンをサポートし、次の構成によって閉じられます

spring.mvc.favicon.enabled=false ## 关闭

デフォルトの表示効果は次のとおりです。

2.ファビコンを閉じる

3.カスタムファビコン

カスタマイズしたfavicon.ico(固定ファイル名)を次のディレクトリに配置して、ファビコンをカスタマイズします。

  • クラスパスルート
  • クラスパスMETA-INF /リソース/
  • クラスパスリソース/
  • クラスパス静的/
  • クラスパスpublic /

要約: 

     ただし、Spring Bootプロジェクトの問題では、デフォルトのファビコンが提供されている場合、Webサイト情報が漏洩する可能性があることが提案されています。ユーザーがカスタムFavicon設定を行わず、Spring Bootプロジェクトが上記のデフォルトアイコンを提供する場合、それは必然的にWebサイトの開発フレームワークの開示につながります。

     したがって、Spring Boot2.2.xでは、デフォルトのfavicon.icoが削除され、上記のapplication.propertiesのプロパティ構成は提供されなくなりました。詳細については、対応する問題を参照してください:https : //github.com/spring-projects/spring-boot/issues/17925

 

元の記事を108件公開 58のような 50,000以上の訪問

おすすめ

転載: blog.csdn.net/qq_41893274/article/details/104761933