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 @   不写代码想写诗的虫子  阅读(89)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示