(6) Spring Boot and Web development

Premise:    Spring Boot 2.2.5 and Web development Official website documentation                                                                            

Make good use of Spring Boot's three axes:

  •  Create a Spring Boot application and select the modules we need;
  • Spring Boot has configured these scenarios by default, only need to specify a small amount of configuration in the configuration file to run;
  • Write business code yourself;  

   Official website directory reference:

                                                 

First, the principle of automatic configuration?

  What does Spring Boot configure for us in this scenario? Can it be modified? What configurations can be modified? Can it be expanded? XXX

     

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

Second, Spring Boot's mapping rules for static resources

Set parameters related to static resources;   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) All  "/ webjars / **"   , go to "classpath: / META-INF / resources / webjars /" to find resources

         -webjars: Introduce static resources on the official website resources  in the form of jar packages               

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

 

               

(2) staticPathPattern = "/ **" to     access any resources of the current project [folder of static resources]

  •  "classpath:/META-INF/resources/"
  •  "classpath:/resources/"
  •  "classpath:/static/",
  • "classpath:/public/"
  • "/": The current project's changing path

Conclusion: The access method is: localhost: 8080 / folder name / ***    Go to the static resource folder to find the corresponding static resource under the folder path

(3) Welcome page: all index.html pages in the static resource folder; mapped by "/ **"

      So the welcome page is to find index.html at localhost: 8080 /  

(4)  Analysis of favicon.ico icon configuration   link springboot2.2.5 Reasons for removing favicon.ico icon

        This Spring Boot project also provides support, but different versions are different. Most of the articles on the Internet have been invalidated in the latest version. This article takes everyone to see the usage in the Spring Boot 2.x version.

Different versions of Spring Boot support Favicon

In the earlier version, Spring Boot supports Favicon by default, and it is closed by the following configuration

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

The default display effect is as follows:

2. Close Favicon

3. Custom Favicon

Place the customized favicon.ico (fixed file name) in the following directory, you can customize Favicon:

  • Classpath root
  • Class path META-INF / resources /
  • Classpath resources /
  • Classpath static /
  • Classpath public /

to sum up: 

     However, in the issues of the Spring Boot project, it is proposed that if the default Favicon is provided, the website information may be leaked. If the user does not make custom Favicon settings, and the Spring Boot project will provide the default icon above, then it will inevitably lead to the disclosure of the website's development framework.

     Therefore, in Spring Boot2.2.x, the default favicon.ico is removed, and the property configuration in the above application.properties is no longer provided. For more detailed information, please refer to the corresponding issue: https://github.com/spring-projects/spring-boot/issues/17925

 

Published 108 original articles · Like 58 · Visits 50,000+

Guess you like

Origin blog.csdn.net/qq_41893274/article/details/104761933