Search in sources :

Example 6 with ViewResolverComposite

use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method mvcViewResolverWithExistingResolver.

@Test
public void mvcViewResolverWithExistingResolver() throws Exception {
    ApplicationContext context = initContext(WebConfig.class, ViewResolverConfig.class);
    ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);
    assertThat(resolver).isNotNull();
    assertThat(resolver.getViewResolvers().size()).isEqualTo(0);
    assertThat(resolver.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);
    assertThat(resolver.resolveViewName("anyViewName", Locale.ENGLISH)).isNull();
}
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)

Example 7 with ViewResolverComposite

use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method mvcViewResolverWithOrderSet.

@Test
public void mvcViewResolverWithOrderSet() {
    ApplicationContext context = initContext(CustomViewResolverOrderConfig.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(123);
}
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)

Example 8 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(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager) {
    ViewResolverRegistry registry = new ViewResolverRegistry(contentNegotiationManager, this.applicationContext);
    configureViewResolvers(registry);
    if (registry.getViewResolvers().isEmpty() && this.applicationContext != null) {
        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());
    if (this.applicationContext != null) {
        composite.setApplicationContext(this.applicationContext);
    }
    if (this.servletContext != null) {
        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 9 with ViewResolverComposite

use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewResolutionWithContentNegotiation.

@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
    loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml");
    ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
    assertThat(compositeResolver).isNotNull();
    assertThat(compositeResolver.getViewResolvers().size()).isEqualTo(1);
    assertThat(compositeResolver.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
    List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
    assertThat(resolvers.get(0).getClass()).isEqualTo(ContentNegotiatingViewResolver.class);
    ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
    assertThat(cnvr.getViewResolvers().size()).isEqualTo(5);
    assertThat(cnvr.getDefaultViews().size()).isEqualTo(1);
    assertThat(cnvr.isUseNotAcceptableStatusCode()).isTrue();
    String beanName = "contentNegotiationManager";
    DirectFieldAccessor accessor = new DirectFieldAccessor(cnvr);
    ContentNegotiationManager manager = (ContentNegotiationManager) accessor.getPropertyValue(beanName);
    assertThat(manager).isNotNull();
    assertThat(this.appContext.getBean(ContentNegotiationManager.class)).isSameAs(manager);
    assertThat(this.appContext.getBean("mvcContentNegotiationManager")).isSameAs(manager);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) 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 10 with ViewResolverComposite

use of org.springframework.web.servlet.view.ViewResolverComposite in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewResolution.

@Test
public void testViewResolution() throws Exception {
    loadBeanDefinitions("mvc-config-view-resolution.xml");
    ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
    assertThat(compositeResolver).isNotNull();
    assertThat(compositeResolver.getViewResolvers().size()).as("Actual: " + compositeResolver.getViewResolvers()).isEqualTo(7);
    assertThat(compositeResolver.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);
    List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
    assertThat(resolvers.get(0).getClass()).isEqualTo(BeanNameViewResolver.class);
    ViewResolver resolver = resolvers.get(1);
    assertThat(resolver.getClass()).isEqualTo(InternalResourceViewResolver.class);
    DirectFieldAccessor accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("viewClass")).isEqualTo(InternalResourceView.class);
    resolver = resolvers.get(2);
    assertThat(resolver).isInstanceOf(FreeMarkerViewResolver.class);
    accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("prefix")).isEqualTo("freemarker-");
    assertThat(accessor.getPropertyValue("suffix")).isEqualTo(".freemarker");
    assertThat((String[]) accessor.getPropertyValue("viewNames")).isEqualTo(new String[] { "my*", "*Report" });
    assertThat(accessor.getPropertyValue("cacheLimit")).isEqualTo(1024);
    resolver = resolvers.get(3);
    assertThat(resolver).isInstanceOf(GroovyMarkupViewResolver.class);
    accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("prefix")).isEqualTo("");
    assertThat(accessor.getPropertyValue("suffix")).isEqualTo(".tpl");
    assertThat(accessor.getPropertyValue("cacheLimit")).isEqualTo(1024);
    resolver = resolvers.get(4);
    assertThat(resolver).isInstanceOf(ScriptTemplateViewResolver.class);
    accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("prefix")).isEqualTo("");
    assertThat(accessor.getPropertyValue("suffix")).isEqualTo("");
    assertThat(accessor.getPropertyValue("cacheLimit")).isEqualTo(1024);
    assertThat(resolvers.get(5).getClass()).isEqualTo(InternalResourceViewResolver.class);
    assertThat(resolvers.get(6).getClass()).isEqualTo(InternalResourceViewResolver.class);
    FreeMarkerConfigurer freeMarkerConfigurer = appContext.getBean(FreeMarkerConfigurer.class);
    assertThat(freeMarkerConfigurer).isNotNull();
    accessor = new DirectFieldAccessor(freeMarkerConfigurer);
    assertThat((String[]) accessor.getPropertyValue("templateLoaderPaths")).isEqualTo(new String[] { "/", "/test" });
    GroovyMarkupConfigurer groovyMarkupConfigurer = appContext.getBean(GroovyMarkupConfigurer.class);
    assertThat(groovyMarkupConfigurer).isNotNull();
    assertThat(groovyMarkupConfigurer.getResourceLoaderPath()).isEqualTo("/test");
    assertThat(groovyMarkupConfigurer.isAutoIndent()).isTrue();
    assertThat(groovyMarkupConfigurer.isCacheTemplates()).isFalse();
    ScriptTemplateConfigurer scriptTemplateConfigurer = appContext.getBean(ScriptTemplateConfigurer.class);
    assertThat(scriptTemplateConfigurer).isNotNull();
    assertThat(scriptTemplateConfigurer.getRenderFunction()).isEqualTo("render");
    assertThat(scriptTemplateConfigurer.getContentType()).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
    assertThat(scriptTemplateConfigurer.getCharset()).isEqualTo(StandardCharsets.ISO_8859_1);
    assertThat(scriptTemplateConfigurer.getResourceLoaderPath()).isEqualTo("classpath:");
    assertThat(scriptTemplateConfigurer.isSharedEngine()).isFalse();
    String[] scripts = { "org/springframework/web/servlet/view/script/nashorn/render.js" };
    accessor = new DirectFieldAccessor(scriptTemplateConfigurer);
    assertThat((String[]) accessor.getPropertyValue("scripts")).isEqualTo(scripts);
}
Also used : GroovyMarkupConfigurer(org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ScriptTemplateConfigurer(org.springframework.web.servlet.view.script.ScriptTemplateConfigurer) 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) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) 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