mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Merge pull request #40874 from rohitp-a
* gh-40874: Polish "Make it easier to override RequestToViewNameTranslator bean" Make it easier to override RequestToViewNameTranslator bean Closes gh-40874
This commit is contained in:
commit
4f6860f2fd
|
@ -95,6 +95,7 @@ import org.springframework.web.servlet.DispatcherServlet;
|
||||||
import org.springframework.web.servlet.FlashMapManager;
|
import org.springframework.web.servlet.FlashMapManager;
|
||||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||||
import org.springframework.web.servlet.LocaleResolver;
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
import org.springframework.web.servlet.RequestToViewNameTranslator;
|
||||||
import org.springframework.web.servlet.View;
|
import org.springframework.web.servlet.View;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
|
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
|
||||||
|
@ -469,6 +470,13 @@ public class WebMvcAutoConfiguration {
|
||||||
return super.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() {
|
private Resource getIndexHtmlResource() {
|
||||||
for (String location : this.resourceProperties.getStaticLocations()) {
|
for (String location : this.resourceProperties.getStaticLocations()) {
|
||||||
Resource indexHtml = getIndexHtmlResource(location);
|
Resource indexHtml = getIndexHtmlResource(location);
|
||||||
|
|
|
@ -102,6 +102,7 @@ import org.springframework.web.servlet.HandlerAdapter;
|
||||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
import org.springframework.web.servlet.LocaleResolver;
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
import org.springframework.web.servlet.RequestToViewNameTranslator;
|
||||||
import org.springframework.web.servlet.View;
|
import org.springframework.web.servlet.View;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
|
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
|
||||||
|
@ -136,6 +137,7 @@ import org.springframework.web.servlet.support.AbstractFlashMapManager;
|
||||||
import org.springframework.web.servlet.support.SessionFlashMapManager;
|
import org.springframework.web.servlet.support.SessionFlashMapManager;
|
||||||
import org.springframework.web.servlet.view.AbstractView;
|
import org.springframework.web.servlet.view.AbstractView;
|
||||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||||
|
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
|
||||||
import org.springframework.web.util.UrlPathHelper;
|
import org.springframework.web.util.UrlPathHelper;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -404,6 +406,26 @@ class WebMvcAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Test
|
||||||
void defaultDateFormat() {
|
void defaultDateFormat() {
|
||||||
this.contextRunner.run((context) -> {
|
this.contextRunner.run((context) -> {
|
||||||
|
@ -1458,6 +1480,15 @@ class WebMvcAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class CustomViewNameTranslator implements RequestToViewNameTranslator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getViewName(HttpServletRequest requestAttributes) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
static class ResourceHandlersWithChildAndParentContextConfiguration {
|
static class ResourceHandlersWithChildAndParentContextConfiguration {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user