Skip to content

Commit

Permalink
Merge pull request #40874 from rohitp-a
Browse files Browse the repository at this point in the history
* gh-40874:
  Polish "Make it easier to override RequestToViewNameTranslator bean"
  Make it easier to override RequestToViewNameTranslator bean

Closes gh-40874
  • Loading branch information
wilkinsona committed Jun 27, 2024
2 parents 70fd788 + 7e04ac2 commit 4f6860f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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,13 @@ 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 @@ -102,6 +102,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 @@ -136,6 +137,7 @@
import org.springframework.web.servlet.support.SessionFlashMapManager;
import org.springframework.web.servlet.view.AbstractView;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
import org.springframework.web.util.UrlPathHelper;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -404,6 +406,26 @@ void customFlashMapManagerWithDifferentNameDoesNotReplaceDefaultFlashMapManager(
});
}

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

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

@Test
void defaultDateFormat() {
this.contextRunner.run((context) -> {
Expand Down Expand Up @@ -1458,6 +1480,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

0 comments on commit 4f6860f

Please sign in to comment.