use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.
the class WebMvcConfigurationSupport method mvcViewResolver.
/**
* Register a {@link ViewResolverComposite} that contains a chain of view resolvers
* to use for view resolution.
* By default this resolver is ordered at 0 unless content negotiation view
* resolution is used in which case the order is raised to
* {@link org.springframework.core.Ordered#HIGHEST_PRECEDENCE
* Ordered.HIGHEST_PRECEDENCE}.
* <p>If no other resolvers are configured,
* {@link ViewResolverComposite#resolveViewName(String, Locale)} returns null in order
* to allow other potential {@link ViewResolver} beans to resolve views.
* @since 4.1
*/
@Bean
public ViewResolver mvcViewResolver() {
ViewResolverRegistry registry = new ViewResolverRegistry();
registry.setContentNegotiationManager(mvcContentNegotiationManager());
registry.setApplicationContext(this.applicationContext);
configureViewResolvers(registry);
if (registry.getViewResolvers().isEmpty()) {
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.applicationContext, ViewResolver.class, true, false);
if (names.length == 1) {
registry.getViewResolvers().add(new InternalResourceViewResolver());
}
}
ViewResolverComposite composite = new ViewResolverComposite();
composite.setOrder(registry.getOrder());
composite.setViewResolvers(registry.getViewResolvers());
composite.setApplicationContext(this.applicationContext);
composite.setServletContext(this.servletContext);
return composite;
}
use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.
the class MvcNamespaceTests method testContentNegotiationManager.
@Test
public void testContentNegotiationManager() throws Exception {
loadBeanDefinitions("mvc-config-content-negotiation-manager.xml");
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setParameter("format", "xml");
NativeWebRequest webRequest = new ServletWebRequest(request);
assertThat(manager.resolveMediaTypes(webRequest)).containsExactly(MediaType.valueOf("application/rss+xml"));
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
assertThat(compositeResolver).isNotNull();
assertThat(compositeResolver.getViewResolvers().size()).as("Actual: " + compositeResolver.getViewResolvers()).isEqualTo(1);
ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
assertThat(resolver.getClass()).isEqualTo(ContentNegotiatingViewResolver.class);
ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
assertThat(cnvr.getContentNegotiationManager()).isSameAs(manager);
}
use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.
the class MvcNamespaceTests method testViewResolutionWithOrderSet.
@Test
public void testViewResolutionWithOrderSet() throws Exception {
loadBeanDefinitions("mvc-config-view-resolution-custom-order.xml");
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
assertThat(compositeResolver).isNotNull();
assertThat(compositeResolver.getViewResolvers().size()).as("Actual: " + compositeResolver.getViewResolvers()).isEqualTo(1);
assertThat(compositeResolver.getOrder()).isEqualTo(123);
}
use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method viewResolvers.
@SuppressWarnings("unchecked")
@Test
public void viewResolvers() throws Exception {
ViewResolverComposite viewResolver = (ViewResolverComposite) this.config.mvcViewResolver(this.config.mvcContentNegotiationManager());
assertThat(viewResolver.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
List<ViewResolver> viewResolvers = viewResolver.getViewResolvers();
DirectFieldAccessor accessor = new DirectFieldAccessor(viewResolvers.get(0));
assertThat(viewResolvers.size()).isEqualTo(1);
assertThat(viewResolvers.get(0).getClass()).isEqualTo(ContentNegotiatingViewResolver.class);
assertThat((boolean) (Boolean) accessor.getPropertyValue("useNotAcceptableStatusCode")).isFalse();
assertThat(accessor.getPropertyValue("contentNegotiationManager")).isNotNull();
List<View> defaultViews = (List<View>) accessor.getPropertyValue("defaultViews");
assertThat(defaultViews).isNotNull();
assertThat(defaultViews.size()).isEqualTo(1);
assertThat(defaultViews.get(0).getClass()).isEqualTo(MappingJackson2JsonView.class);
viewResolvers = (List<ViewResolver>) accessor.getPropertyValue("viewResolvers");
assertThat(viewResolvers).isNotNull();
assertThat(viewResolvers.size()).isEqualTo(1);
assertThat(viewResolvers.get(0).getClass()).isEqualTo(InternalResourceViewResolver.class);
accessor = new DirectFieldAccessor(viewResolvers.get(0));
assertThat(accessor.getPropertyValue("prefix")).isEqualTo("/");
assertThat(accessor.getPropertyValue("suffix")).isEqualTo(".jsp");
}
use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportTests method mvcViewResolver.
@Test
public void mvcViewResolver() {
ApplicationContext context = initContext(WebConfig.class);
ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);
assertThat(resolver).isNotNull();
assertThat(resolver.getViewResolvers().size()).isEqualTo(1);
assertThat(resolver.getViewResolvers().get(0).getClass()).isEqualTo(InternalResourceViewResolver.class);
assertThat(resolver.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);
}
Aggregations