springboot首页定制

源码分析

  • WebMvcAutoConfiguration
    • EnableWebMvcConfiguration
      • welcomePageHandlerMapping()
        @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
              FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
           WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
                 new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
                 this.mvcProperties.getStaticPathPattern());
           welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
           return welcomePageHandlerMapping;
        }
        
        private Optional<Resource> getWelcomePage() {
           String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
           return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
        }
        
        private Resource getIndexHtml(String location) {
           return this.resourceLoader.getResource(location + "index.html");
        }
        
        private boolean isReadable(Resource resource) {
           try {
              return resource.exists() && (resource.getURL() != null);
           }
           catch (Exception ex) {
              return false;
           }
        }
        

thymeleaf主页定制

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
}

注意点:

  1. 所有页面的静态资源都需要使用thymeleaf接管
  2. url: @{}
posted @ 2022-04-11 11:38  不写代码想写诗的虫子  阅读(96)  评论(0)    收藏  举报