Search in sources :

Example 11 with ViewResolver

use of org.springframework.web.servlet.ViewResolver in project grails-core by grails.

the class GrailsExceptionResolver method resolveView.

protected void resolveView(HttpServletRequest request, UrlMappingInfo info, ModelAndView mv) throws Exception {
    ViewResolver viewResolver = WebUtils.lookupViewResolver(servletContext);
    View v = UrlMappingUtils.resolveView(request, info, info.getViewName(), viewResolver);
    if (v != null) {
        mv.setView(v);
    }
}
Also used : ViewResolver(org.springframework.web.servlet.ViewResolver) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View)

Example 12 with ViewResolver

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

the class MvcNamespaceTests method testViewResolution.

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

Example 13 with ViewResolver

use of org.springframework.web.servlet.ViewResolver 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", 6);
    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"));
}
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) TilesViewResolver(org.springframework.web.servlet.view.tiles3.TilesViewResolver) Test(org.junit.Test)

Example 14 with ViewResolver

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

the class ContentNegotiatingViewResolverTests method resolveViewNameWithRequestParameter.

@Test
public void resolveViewNameWithRequestParameter() throws Exception {
    request.addParameter("format", "xls");
    Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
    ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy));
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    View viewMock = mock(View.class, "application_xls");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MediaType(org.springframework.http.MediaType) ParameterContentNegotiationStrategy(org.springframework.web.accept.ParameterContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Example 15 with ViewResolver

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

the class ContentNegotiatingViewResolverTests method resolveViewNameFilename.

@Test
public void resolveViewNameFilename() throws Exception {
    request.setRequestURI("/test.html");
    ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
    ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
    viewResolver.afterPropertiesSet();
    View viewMock1 = mock(View.class, "application_xml");
    View viewMock2 = mock(View.class, "text_html");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
    given(viewResolverMock1.resolveViewName(viewName + ".html", locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock2.resolveViewName(viewName + ".html", locale)).willReturn(viewMock2);
    given(viewMock1.getContentType()).willReturn("application/xml");
    given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock2, result);
}
Also used : Locale(java.util.Locale) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Aggregations

ViewResolver (org.springframework.web.servlet.ViewResolver)21 Test (org.junit.Test)18 View (org.springframework.web.servlet.View)17 Locale (java.util.Locale)14 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)7 MediaType (org.springframework.http.MediaType)6 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)4 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)4 ViewResolverComposite (org.springframework.web.servlet.view.ViewResolverComposite)4 ArrayList (java.util.ArrayList)3 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 TilesViewResolver (org.springframework.web.servlet.view.tiles3.TilesViewResolver)3 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)2 List (java.util.List)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1