org.springframework.web.servlet.view.ViewResolverComposite Java Examples

The following examples show how to use org.springframework.web.servlet.view.ViewResolverComposite. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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.xml");
	NativeWebRequest webRequest = new ServletWebRequest(request);
	assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")),
			manager.resolveMediaTypes(webRequest));

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());

	ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
	assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
	assertSame(manager, cnvr.getContentNegotiationManager());
}
 
Example #2
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml");

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals(1, compositeResolver.getViewResolvers().size());
	assertEquals(Ordered.HIGHEST_PRECEDENCE, compositeResolver.getOrder());

	List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
	assertEquals(ContentNegotiatingViewResolver.class, resolvers.get(0).getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
	assertEquals(6, cnvr.getViewResolvers().size());
	assertEquals(1, cnvr.getDefaultViews().size());
	assertTrue(cnvr.isUseNotAcceptableStatusCode());

	String beanName = "contentNegotiationManager";
	DirectFieldAccessor accessor = new DirectFieldAccessor(cnvr);
	ContentNegotiationManager manager = (ContentNegotiationManager) accessor.getPropertyValue(beanName);
	assertNotNull(manager);
	assertSame(manager, this.appContext.getBean(ContentNegotiationManager.class));
	assertSame(manager, this.appContext.getBean("mvcContentNegotiationManager"));
}
 
Example #3
Source File: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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(ContentNegotiationManager mvcContentNegotiationManager) {
	ViewResolverRegistry registry = new ViewResolverRegistry(
			mvcContentNegotiationManager, 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;
}
 
Example #4
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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(
			mvcContentNegotiationManager(), 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;
}
 
Example #5
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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.xml");
	NativeWebRequest webRequest = new ServletWebRequest(request);
	assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")),
			manager.resolveMediaTypes(webRequest));

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());

	ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
	assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
	assertSame(manager, cnvr.getContentNegotiationManager());
}
 
Example #6
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml");

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals(1, compositeResolver.getViewResolvers().size());
	assertEquals(Ordered.HIGHEST_PRECEDENCE, compositeResolver.getOrder());

	List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
	assertEquals(ContentNegotiatingViewResolver.class, resolvers.get(0).getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
	assertEquals(6, cnvr.getViewResolvers().size());
	assertEquals(1, cnvr.getDefaultViews().size());
	assertTrue(cnvr.isUseNotAcceptableStatusCode());

	String beanName = "contentNegotiationManager";
	DirectFieldAccessor accessor = new DirectFieldAccessor(cnvr);
	ContentNegotiationManager manager = (ContentNegotiationManager) accessor.getPropertyValue(beanName);
	assertNotNull(manager);
	assertSame(manager, this.appContext.getBean(ContentNegotiationManager.class));
	assertSame(manager, this.appContext.getBean("mvcContentNegotiationManager"));
}
 
Example #7
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml", 7);

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals(1, compositeResolver.getViewResolvers().size());
	assertEquals(Ordered.HIGHEST_PRECEDENCE, compositeResolver.getOrder());

	List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
	assertEquals(ContentNegotiatingViewResolver.class, resolvers.get(0).getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
	assertEquals(7, cnvr.getViewResolvers().size());
	assertEquals(1, cnvr.getDefaultViews().size());
	assertTrue(cnvr.isUseNotAcceptableStatusCode());

	String beanName = "contentNegotiationManager";
	DirectFieldAccessor accessor = new DirectFieldAccessor(cnvr);
	ContentNegotiationManager manager = (ContentNegotiationManager) accessor.getPropertyValue(beanName);
	assertNotNull(manager);
	assertSame(manager, this.appContext.getBean(ContentNegotiationManager.class));
}
 
Example #8
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testContentNegotiationManager() throws Exception {
	loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 15);

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
	NativeWebRequest webRequest = new ServletWebRequest(request);
	assertEquals(Arrays.asList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());

	ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
	assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
	assertSame(manager, cnvr.getContentNegotiationManager());
}
 
Example #9
Source File: WebMvcConfigurationSupport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #10
Source File: WebMvcConfigurationSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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(
			mvcContentNegotiationManager(), 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;
}
 
Example #11
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void mvcViewResolverWithOrderSet() {
	ApplicationContext context = initContext(CustomViewResolverOrderConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(1, resolver.getViewResolvers().size());
	assertEquals(InternalResourceViewResolver.class, resolver.getViewResolvers().get(0).getClass());
	assertEquals(123, resolver.getOrder());
}
 
Example #12
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void mvcViewResolverWithExistingResolver() throws Exception {
	ApplicationContext context = initContext(WebConfig.class, ViewResolverConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(0, resolver.getViewResolvers().size());
	assertEquals(Ordered.LOWEST_PRECEDENCE, resolver.getOrder());
	assertNull(resolver.resolveViewName("anyViewName", Locale.ENGLISH));
}
 
Example #13
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void mvcViewResolver() {
	ApplicationContext context = initContext(WebConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(1, resolver.getViewResolvers().size());
	assertEquals(InternalResourceViewResolver.class, resolver.getViewResolvers().get(0).getClass());
	assertEquals(Ordered.LOWEST_PRECEDENCE, resolver.getOrder());
}
 
Example #14
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testViewResolutionWithOrderSet() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-custom-order.xml", 1);

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());
	assertEquals(123, compositeResolver.getOrder());
}
 
Example #15
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void mvcViewResolverWithOrderSet() {
	ApplicationContext context = initContext(CustomViewResolverOrderConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(1, resolver.getViewResolvers().size());
	assertEquals(InternalResourceViewResolver.class, resolver.getViewResolvers().get(0).getClass());
	assertEquals(123, resolver.getOrder());
}
 
Example #16
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void mvcViewResolverWithExistingResolver() throws Exception {
	ApplicationContext context = initContext(WebConfig.class, ViewResolverConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(0, resolver.getViewResolvers().size());
	assertEquals(Ordered.LOWEST_PRECEDENCE, resolver.getOrder());
	assertNull(resolver.resolveViewName("anyViewName", Locale.ENGLISH));
}
 
Example #17
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void mvcViewResolver() {
	ApplicationContext context = initContext(WebConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(1, resolver.getViewResolvers().size());
	assertEquals(InternalResourceViewResolver.class, resolver.getViewResolvers().get(0).getClass());
	assertEquals(Ordered.LOWEST_PRECEDENCE, resolver.getOrder());
}
 
Example #18
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testViewResolutionWithOrderSet() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-custom-order.xml");

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());
	assertEquals(123, compositeResolver.getOrder());
}
 
Example #19
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void mvcViewResolverWithOrderSet() {
	ApplicationContext context = initContext(CustomViewResolverOrderConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(1, resolver.getViewResolvers().size());
	assertEquals(InternalResourceViewResolver.class, resolver.getViewResolvers().get(0).getClass());
	assertEquals(123, resolver.getOrder());
}
 
Example #20
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void mvcViewResolverWithExistingResolver() throws Exception {
	ApplicationContext context = initContext(WebConfig.class, ViewResolverConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(0, resolver.getViewResolvers().size());
	assertEquals(Ordered.LOWEST_PRECEDENCE, resolver.getOrder());
	assertNull(resolver.resolveViewName("anyViewName", Locale.ENGLISH));
}
 
Example #21
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void mvcViewResolver() {
	ApplicationContext context = initContext(WebConfig.class);
	ViewResolverComposite resolver = context.getBean("mvcViewResolver", ViewResolverComposite.class);

	assertNotNull(resolver);
	assertEquals(1, resolver.getViewResolvers().size());
	assertEquals(InternalResourceViewResolver.class, resolver.getViewResolvers().get(0).getClass());
	assertEquals(Ordered.LOWEST_PRECEDENCE, resolver.getOrder());
}
 
Example #22
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testViewResolutionWithOrderSet() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-custom-order.xml");

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());
	assertEquals(123, compositeResolver.getOrder());
}