Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to provide a custom RequestToViewNameTranslator #40874

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.springframework.web.servlet.FlashMapManager;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.RequestToViewNameTranslator;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
Expand Down Expand Up @@ -469,6 +470,15 @@ public FlashMapManager flashMapManager() {
return super.flashMapManager();
}


@Override
@Bean
@ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME)
public RequestToViewNameTranslator viewNameTranslator() {
return super.viewNameTranslator();
}


private Resource getIndexHtmlResource() {
for (String location : this.resourceProperties.getStaticLocations()) {
Resource indexHtml = getIndexHtmlResource(location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand All @@ -89,6 +90,7 @@
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.FormContentFilter;
Expand All @@ -102,6 +104,7 @@
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.RequestToViewNameTranslator;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
Expand Down Expand Up @@ -404,6 +407,24 @@ void customFlashMapManagerWithDifferentNameDoesNotReplaceDefaultFlashMapManager(
});
}

@Test
public void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() {
this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
.run((context) -> {
assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
assertThat(context.getBean("viewNameTranslator")).isInstanceOf(SessionFlashMapManager.class);
});
}

@Test
void customViewNameTranslatorWithDifferentNameReplaceDefaultViewNameTranslator() {
this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
.run((context) -> {
assertThat(context).hasSingleBean(RequestToViewNameTranslator.class);
assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
});
}

@Test
void defaultDateFormat() {
this.contextRunner.run((context) -> {
Expand Down Expand Up @@ -1458,6 +1479,15 @@ protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest requ

}

static class CustomViewNameTranslator implements RequestToViewNameTranslator {

@Override
public String getViewName(HttpServletRequest requestAttributes) {
return null;
}

}

@Configuration(proxyBeanMethods = false)
static class ResourceHandlersWithChildAndParentContextConfiguration {

Expand Down