use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolver method getCandidateViews.
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception {
List<View> candidateViews = new ArrayList<>();
for (ViewResolver viewResolver : this.viewResolvers) {
View view = viewResolver.resolveViewName(viewName, locale);
if (view != null) {
candidateViews.add(view);
}
for (MediaType requestedMediaType : requestedMediaTypes) {
List<String> extensions = this.contentNegotiationManager.resolveFileExtensions(requestedMediaType);
for (String extension : extensions) {
String viewNameWithExtension = viewName + '.' + extension;
view = viewResolver.resolveViewName(viewNameWithExtension, locale);
if (view != null) {
candidateViews.add(view);
}
}
}
}
if (!CollectionUtils.isEmpty(this.defaultViews)) {
candidateViews.addAll(this.defaultViews);
}
return candidateViews;
}
use of org.springframework.web.servlet.ViewResolver 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.ViewResolver 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.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNameAcceptHeaderWithSuffix.
// SPR-9807
@Test
public void resolveViewNameAcceptHeaderWithSuffix() throws Exception {
request.addHeader("Accept", "application/vnd.example-v2+xml");
ViewResolver viewResolverMock = mock(ViewResolver.class);
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock));
viewResolver.afterPropertiesSet();
View viewMock = mock(View.class, "application_xml");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
given(viewMock.getContentType()).willReturn("application/*+xml");
View result = viewResolver.resolveViewName(viewName, locale);
assertThat(result).as("Invalid view").isSameAs(viewMock);
assertThat(request.getAttribute(View.SELECTED_CONTENT_TYPE)).isEqualTo(new MediaType("application", "vnd.example-v2+xml"));
}
use of org.springframework.web.servlet.ViewResolver in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewContentTypeNull.
@Test
public void resolveViewContentTypeNull() throws Exception {
request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
ViewResolver viewResolverMock = mock(ViewResolver.class);
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
viewResolver.afterPropertiesSet();
View viewMock = mock(View.class, "application_xml");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
given(viewMock.getContentType()).willReturn(null);
View result = viewResolver.resolveViewName(viewName, locale);
assertThat(result).as("Invalid view").isNull();
}
Aggregations