Search in sources :

Example 1 with ViewResolver

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;
}
Also used : ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) SmartView(org.springframework.web.servlet.SmartView) View(org.springframework.web.servlet.View) ViewResolver(org.springframework.web.servlet.ViewResolver)

Example 2 with ViewResolver

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);
}
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 ViewResolver

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");
}
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 4 with ViewResolver

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"));
}
Also used : Locale(java.util.Locale) MediaType(org.springframework.http.MediaType) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Example 5 with ViewResolver

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();
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Aggregations

ViewResolver (org.springframework.web.servlet.ViewResolver)24 Test (org.junit.jupiter.api.Test)20 View (org.springframework.web.servlet.View)19 Locale (java.util.Locale)15 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)9 MediaType (org.springframework.http.MediaType)6 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)5 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)5 ArrayList (java.util.ArrayList)4 ViewResolverComposite (org.springframework.web.servlet.view.ViewResolverComposite)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)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 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)2 ParameterContentNegotiationStrategy (org.springframework.web.accept.ParameterContentNegotiationStrategy)2 ArticleRssFeedViewResolver (com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver)1 List (java.util.List)1 Bean (org.springframework.context.annotation.Bean)1