diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index c62b3eef332..6dd2ea43d9e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -48,6 +48,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; +import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain; @@ -57,6 +58,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter; import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter; import org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter; +import org.springframework.context.ApplicationContext; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -130,6 +132,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; * @author EddĂș MelĂ©ndez * @author Stephane Nicoll * @author Kristine Jetzke + * @author Bruce Brouwer */ @Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @@ -330,8 +333,11 @@ public class WebMvcAutoConfiguration { } @Bean - public WelcomePageHandlerMapping welcomePageHandlerMapping() { - return new WelcomePageHandlerMapping(getWelcomePage(), + public WelcomePageHandlerMapping welcomePageHandlerMapping( + ApplicationContext applicationContext) { + return new WelcomePageHandlerMapping( + new TemplateAvailabilityProviders(applicationContext), + applicationContext, getWelcomePage(), this.mvcProperties.getStaticPathPattern()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java index c1887e4f7e3..8c2e7a91b1b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java @@ -24,6 +24,8 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders; +import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -37,19 +39,37 @@ import org.springframework.web.servlet.mvc.ParameterizableViewController; * static page is preferred. * * @author Andy Wilkinson + * @author Bruce Brouwer */ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping { private static final Log logger = LogFactory.getLog(WelcomePageHandlerMapping.class); - WelcomePageHandlerMapping(Optional welcomePage, String staticPathPattern) { - if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) { - logger.info("Adding welcome page: " + welcomePage.get()); - ParameterizableViewController controller = new ParameterizableViewController(); - controller.setViewName("forward:index.html"); - setRootHandler(controller); - setOrder(0); + WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, + ApplicationContext applicationContext, Optional welcomePage, + String staticPathPattern) { + if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) { + logger.info("Adding welcome page template: index"); + setRootViewName("index"); } + else if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) { + logger.info("Adding welcome page: " + welcomePage); + setRootViewName("forward:index.html"); + } + } + + private boolean welcomeTemplateExists( + TemplateAvailabilityProviders templateAvailabilityProviders, + ApplicationContext applicationContext) { + return templateAvailabilityProviders.getProvider("index", + applicationContext) != null; + } + + private void setRootViewName(final String viewName) { + ParameterizableViewController controller = new ParameterizableViewController(); + controller.setViewName(viewName); + setRootHandler(controller); + setOrder(0); } @Override