From d205d9404af309af2ae60f1838dda97030b3ee6b Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 19 Aug 2013 14:10:57 +0100 Subject: [PATCH] Add additional ViewResolver configuration The DispatcherServlet adds a default InternalViewResolver which was used by some apps, but when the actuator was available it added an "/error" bean and effectively switched off the default view resolver. The net fix was to add an InternalViewResolver at the same time as adding any other ViewResolvers. [Fixes #55357516] [bs-290] Actuator UI app cannot serve static index.html --- .../thymeleaf/ThymeleafAutoConfiguration.java | 4 ++++ .../autoconfigure/web/WebMvcAutoConfiguration.java | 12 ++++++++++++ .../web/WebMvcAutoConfigurationTests.java | 11 ----------- .../spring-boot-sample-web-static/pom.xml | 5 +++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index 2e2225f8fc..ebcbbc797c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -34,6 +34,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.thymeleaf.TemplateProcessingParameters; @@ -153,6 +154,9 @@ public class ThymeleafAutoConfiguration { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(this.templateEngine); resolver.setCharacterEncoding("UTF-8"); + // Needs to come before any fallback resolver (e.g. a + // InternalResourceViewResolver) + resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20); return resolver; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index 8a7086632f..e8221bad16 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -58,6 +58,7 @@ import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.view.BeanNameViewResolver; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; +import org.springframework.web.servlet.view.InternalResourceViewResolver; /** * {@link EnableAutoConfiguration Auto-configuration} for {@link EnableWebMvc Web MVC}. @@ -110,6 +111,14 @@ public class WebMvcAutoConfiguration { @Autowired private ResourceLoader resourceLoader; + @ConditionalOnBean(View.class) + @ConditionalOnMissingBean(InternalResourceViewResolver.class) + @Bean + public InternalResourceViewResolver defaultViewResolver() { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + return resolver; + } + @ConditionalOnBean(View.class) @Bean public BeanNameViewResolver beanNameViewResolver() { @@ -124,6 +133,9 @@ public class WebMvcAutoConfiguration { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); resolver.setContentNegotiationManager(beanFactory .getBean(ContentNegotiationManager.class)); + // ContentNegotiatingViewResolver uses all the other view resolvers to locate + // a view so it should have a high precedence + resolver.setOrder(Ordered.HIGHEST_PRECEDENCE); return resolver; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index c0d71bdeae..648f86ae55 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -25,7 +25,6 @@ import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; @@ -35,7 +34,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.View; -import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.view.AbstractView; import static org.junit.Assert.assertEquals; @@ -78,15 +76,6 @@ public class WebMvcAutoConfigurationTests { assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length); } - @Test - public void viewResolversCreatedIfViewsPresent() throws Exception { - this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, ViewConfig.class, - WebMvcAutoConfiguration.class); - this.context.refresh(); - assertEquals(2, this.context.getBeanNamesForType(ViewResolver.class).length); - } - @Configuration protected static class ViewConfig { diff --git a/spring-boot-samples/spring-boot-sample-web-static/pom.xml b/spring-boot-samples/spring-boot-sample-web-static/pom.xml index 1f42294a83..cef97fa519 100644 --- a/spring-boot-samples/spring-boot-sample-web-static/pom.xml +++ b/spring-boot-samples/spring-boot-sample-web-static/pom.xml @@ -19,6 +19,11 @@ spring-boot-starter-web ${project.version} + + ${project.groupId} + spring-boot-starter-actuator + ${project.version} + ${project.groupId} spring-boot-starter-tomcat