|
108 | 108 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
109 | 109 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
110 | 110 | import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
|
| 111 | +import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; |
111 | 112 | import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
112 | 113 | import org.springframework.web.servlet.i18n.FixedLocaleResolver;
|
113 | 114 | import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
|
@@ -437,12 +438,29 @@ protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter() {
|
437 | 438 | @Bean
|
438 | 439 | public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
|
439 | 440 | FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
|
440 |
| - WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping( |
441 |
| - new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), |
442 |
| - this.mvcProperties.getStaticPathPattern()); |
443 |
| - welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); |
444 |
| - welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations()); |
445 |
| - return welcomePageHandlerMapping; |
| 441 | + return createWelcomePageHandlerMapping(applicationContext, mvcConversionService, mvcResourceUrlProvider, |
| 442 | + WelcomePageHandlerMapping::new); |
| 443 | + } |
| 444 | + |
| 445 | + @Bean |
| 446 | + public WelcomePageNotAcceptableHandlerMapping welcomePageNotAcceptableHandlerMapping( |
| 447 | + ApplicationContext applicationContext, FormattingConversionService mvcConversionService, |
| 448 | + ResourceUrlProvider mvcResourceUrlProvider) { |
| 449 | + return createWelcomePageHandlerMapping(applicationContext, mvcConversionService, mvcResourceUrlProvider, |
| 450 | + WelcomePageNotAcceptableHandlerMapping::new); |
| 451 | + } |
| 452 | + |
| 453 | + private <T extends AbstractUrlHandlerMapping> T createWelcomePageHandlerMapping( |
| 454 | + ApplicationContext applicationContext, FormattingConversionService mvcConversionService, |
| 455 | + ResourceUrlProvider mvcResourceUrlProvider, WelcomePageHandlerMappingFactory<T> factory) { |
| 456 | + TemplateAvailabilityProviders templateAvailabilityProviders = new TemplateAvailabilityProviders( |
| 457 | + applicationContext); |
| 458 | + String staticPathPattern = this.mvcProperties.getStaticPathPattern(); |
| 459 | + T handlerMapping = factory.create(templateAvailabilityProviders, applicationContext, getIndexHtmlResource(), |
| 460 | + staticPathPattern); |
| 461 | + handlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); |
| 462 | + handlerMapping.setCorsConfigurations(getCorsConfigurations()); |
| 463 | + return handlerMapping; |
446 | 464 | }
|
447 | 465 |
|
448 | 466 | @Override
|
@@ -471,25 +489,25 @@ public FlashMapManager flashMapManager() {
|
471 | 489 | return super.flashMapManager();
|
472 | 490 | }
|
473 | 491 |
|
474 |
| - private Resource getWelcomePage() { |
| 492 | + private Resource getIndexHtmlResource() { |
475 | 493 | for (String location : this.resourceProperties.getStaticLocations()) {
|
476 |
| - Resource indexHtml = getIndexHtml(location); |
| 494 | + Resource indexHtml = getIndexHtmlResource(location); |
477 | 495 | if (indexHtml != null) {
|
478 | 496 | return indexHtml;
|
479 | 497 | }
|
480 | 498 | }
|
481 | 499 | ServletContext servletContext = getServletContext();
|
482 | 500 | if (servletContext != null) {
|
483 |
| - return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION)); |
| 501 | + return getIndexHtmlResource(new ServletContextResource(servletContext, SERVLET_LOCATION)); |
484 | 502 | }
|
485 | 503 | return null;
|
486 | 504 | }
|
487 | 505 |
|
488 |
| - private Resource getIndexHtml(String location) { |
489 |
| - return getIndexHtml(this.resourceLoader.getResource(location)); |
| 506 | + private Resource getIndexHtmlResource(String location) { |
| 507 | + return getIndexHtmlResource(this.resourceLoader.getResource(location)); |
490 | 508 | }
|
491 | 509 |
|
492 |
| - private Resource getIndexHtml(Resource location) { |
| 510 | + private Resource getIndexHtmlResource(Resource location) { |
493 | 511 | try {
|
494 | 512 | Resource resource = location.createRelative("index.html");
|
495 | 513 | if (resource.exists() && (resource.getURL() != null)) {
|
@@ -603,6 +621,15 @@ ResourceChainResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCu
|
603 | 621 |
|
604 | 622 | }
|
605 | 623 |
|
| 624 | + @FunctionalInterface |
| 625 | + interface WelcomePageHandlerMappingFactory<T extends AbstractUrlHandlerMapping> { |
| 626 | + |
| 627 | + T create(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, |
| 628 | + Resource indexHtmlResource, String staticPathPattern); |
| 629 | + |
| 630 | + } |
| 631 | + |
| 632 | + @FunctionalInterface |
606 | 633 | interface ResourceHandlerRegistrationCustomizer {
|
607 | 634 |
|
608 | 635 | void customize(ResourceHandlerRegistration registration);
|
|
0 commit comments