Skip to content

Commit 062ed4b

Browse files
rohitp-awilkinsona
authored andcommitted
Make it easier to override RequestToViewNameTranslator bean
See gh-40874
1 parent 70fd788 commit 062ed4b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import org.springframework.web.servlet.FlashMapManager;
9696
import org.springframework.web.servlet.HandlerExceptionResolver;
9797
import org.springframework.web.servlet.LocaleResolver;
98+
import org.springframework.web.servlet.RequestToViewNameTranslator;
9899
import org.springframework.web.servlet.View;
99100
import org.springframework.web.servlet.ViewResolver;
100101
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
@@ -469,6 +470,15 @@ public FlashMapManager flashMapManager() {
469470
return super.flashMapManager();
470471
}
471472

473+
474+
@Override
475+
@Bean
476+
@ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME)
477+
public RequestToViewNameTranslator viewNameTranslator() {
478+
return super.viewNameTranslator();
479+
}
480+
481+
472482
private Resource getIndexHtmlResource() {
473483
for (String location : this.resourceProperties.getStaticLocations()) {
474484
Resource indexHtml = getIndexHtmlResource(location);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
6666
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
6767
import org.springframework.context.ApplicationContext;
68+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6869
import org.springframework.context.annotation.Bean;
6970
import org.springframework.context.annotation.Configuration;
7071
import org.springframework.context.annotation.Import;
@@ -89,6 +90,7 @@
8990
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
9091
import org.springframework.web.bind.annotation.ControllerAdvice;
9192
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
93+
import org.springframework.web.context.request.RequestAttributes;
9294
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
9395
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
9496
import org.springframework.web.filter.FormContentFilter;
@@ -102,6 +104,7 @@
102104
import org.springframework.web.servlet.HandlerExceptionResolver;
103105
import org.springframework.web.servlet.HandlerMapping;
104106
import org.springframework.web.servlet.LocaleResolver;
107+
import org.springframework.web.servlet.RequestToViewNameTranslator;
105108
import org.springframework.web.servlet.View;
106109
import org.springframework.web.servlet.ViewResolver;
107110
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
@@ -404,6 +407,24 @@ void customFlashMapManagerWithDifferentNameDoesNotReplaceDefaultFlashMapManager(
404407
});
405408
}
406409

410+
@Test
411+
public void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() {
412+
this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
413+
.run((context) -> {
414+
assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
415+
assertThat(context.getBean("viewNameTranslator")).isInstanceOf(SessionFlashMapManager.class);
416+
});
417+
}
418+
419+
@Test
420+
void customViewNameTranslatorWithDifferentNameReplaceDefaultViewNameTranslator() {
421+
this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
422+
.run((context) -> {
423+
assertThat(context).hasSingleBean(RequestToViewNameTranslator.class);
424+
assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
425+
});
426+
}
427+
407428
@Test
408429
void defaultDateFormat() {
409430
this.contextRunner.run((context) -> {
@@ -1458,6 +1479,15 @@ protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest requ
14581479

14591480
}
14601481

1482+
static class CustomViewNameTranslator implements RequestToViewNameTranslator {
1483+
1484+
@Override
1485+
public String getViewName(HttpServletRequest requestAttributes) {
1486+
return null;
1487+
}
1488+
1489+
}
1490+
14611491
@Configuration(proxyBeanMethods = false)
14621492
static class ResourceHandlersWithChildAndParentContextConfiguration {
14631493

0 commit comments

Comments
 (0)