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