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();
}
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);
}
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;
}
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);
}
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);
}
Aggregations