Search in sources :

Example 1 with ViewResolverComposite

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;
}
Also used : ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Bean(org.springframework.context.annotation.Bean)

Example 2 with ViewResolverComposite

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);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) ViewResolver(org.springframework.web.servlet.ViewResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 3 with ViewResolverComposite

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);
}
Also used : ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) Test(org.junit.jupiter.api.Test)

Example 4 with ViewResolverComposite

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");
}
Also used : ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) List(java.util.List) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) ViewResolver(org.springframework.web.servlet.ViewResolver) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Example 5 with ViewResolverComposite

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);
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) Test(org.junit.jupiter.api.Test)

Aggregations

ViewResolverComposite (org.springframework.web.servlet.view.ViewResolverComposite)10 Test (org.junit.jupiter.api.Test)8 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)6 ViewResolver (org.springframework.web.servlet.ViewResolver)4 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ApplicationContext (org.springframework.context.ApplicationContext)3 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)3 BeanNameViewResolver (org.springframework.web.servlet.view.BeanNameViewResolver)3 FreeMarkerViewResolver (org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver)3 GroovyMarkupViewResolver (org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver)3 ScriptTemplateViewResolver (org.springframework.web.servlet.view.script.ScriptTemplateViewResolver)3 Bean (org.springframework.context.annotation.Bean)2 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)2 List (java.util.List)1 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1 View (org.springframework.web.servlet.View)1 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)1 FreeMarkerConfigurer (org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer)1