/** * Prepare for rendering, and determine the request dispatcher path * to forward to (or to include). * <p>This implementation simply returns the configured URL. * Subclasses can override this to determine a resource to render, * typically interpreting the URL in a different manner. * @param request current HTTP request * @param response current HTTP response * @return the request dispatcher path to use * @throws Exception if preparations failed * @see #getUrl() */ protected String prepareForRendering(HttpServletRequest request, HttpServletResponse response) throws Exception { String path = getUrl(); Assert.state(path != null, "'url' not set"); if (this.preventDispatchLoop) { String uri = request.getRequestURI(); if (path.startsWith("/") ? uri.equals(path) : uri.equals(StringUtils.applyRelativePath(uri, path))) { throw new ServletException("Circular view path [" + path + "]: would dispatch back " + "to the current handler URL [" + uri + "] again. Check your ViewResolver setup! " + "(Hint: This may be the result of an unspecified view, due to default view name generation.)"); } } return path; }
throw new ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!"); response.setContentType(getContentType()); if (logger.isDebugEnabled()) { logger.debug("Including [" + getUrl() + "]"); logger.debug("Forwarding to [" + getUrl() + "]");
/** * Prepare for rendering, and determine the request dispatcher path * to forward to (or to include). * <p>This implementation simply returns the configured URL. * Subclasses can override this to determine a resource to render, * typically interpreting the URL in a different manner. * @param request current HTTP request * @param response current HTTP response * @return the request dispatcher path to use * @throws Exception if preparations failed * @see #getUrl() */ protected String prepareForRendering(HttpServletRequest request, HttpServletResponse response) throws Exception { String path = getUrl(); Assert.state(path != null, "'url' not set"); if (this.preventDispatchLoop) { String uri = request.getRequestURI(); if (path.startsWith("/") ? uri.equals(path) : uri.equals(StringUtils.applyRelativePath(uri, path))) { throw new ServletException("Circular view path [" + path + "]: would dispatch back " + "to the current handler URL [" + uri + "] again. Check your ViewResolver setup! " + "(Hint: This may be the result of an unspecified view, due to default view name generation.)"); } } return path; }
throw new ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!"); response.setContentType(getContentType()); if (logger.isDebugEnabled()) { logger.debug("Including [" + getUrl() + "]"); logger.debug("Forwarding to [" + getUrl() + "]");
@Test public void debugViewEnglish() throws Exception { View v = rb.resolveViewName("debugView", Locale.ENGLISH); assertThat(v, instanceOf(InternalResourceView.class)); InternalResourceView jv = (InternalResourceView) v; assertEquals("debugView must have correct URL", "jsp/debug/debug.jsp", jv.getUrl()); Map<String, Object> m = jv.getStaticAttributes(); assertEquals("Must have 2 static attributes", 2, m.size()); assertEquals("attribute foo", "bar", m.get("foo")); assertEquals("attribute postcode", "SE10 9JY", m.get("postcode")); assertEquals("Correct default content type", AbstractView.DEFAULT_CONTENT_TYPE, jv.getContentType()); }
@Test public void debugViewFrench() throws Exception { View v = rb.resolveViewName("debugView", Locale.FRENCH); assertThat(v, instanceOf(InternalResourceView.class)); InternalResourceView jv = (InternalResourceView) v; assertEquals("French debugView must have correct URL", "jsp/debug/deboug.jsp", jv.getUrl()); assertEquals("Correct overridden (XML) content type", "text/xml;charset=ISO-8859-1", jv.getContentType()); }
private void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); vr.setPrefix("/WEB-INF/"); vr.setSuffix(".jsp"); vr.setApplicationContext(wac); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "/WEB-INF/example1.jsp", ((InternalResourceView) view).getUrl()); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "/WEB-INF/example2.jsp", ((InternalResourceView) view).getUrl()); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl()); assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
@Test public void testBeanNameViewResolver() throws ServletException { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); MutablePropertyValues pvs1 = new MutablePropertyValues(); pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp")); wac.registerSingleton("example1", InternalResourceView.class, pvs1); MutablePropertyValues pvs2 = new MutablePropertyValues(); pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp")); wac.registerSingleton("example2", JstlView.class, pvs2); BeanNameViewResolver vr = new BeanNameViewResolver(); vr.setApplicationContext(wac); wac.refresh(); View view = vr.resolveViewName("example1", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl()); view = vr.resolveViewName("example2", Locale.getDefault()); assertEquals("Correct view class", JstlView.class, view.getClass()); assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl()); }
assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl()); Map<String, Object> attributes = ((InternalResourceView) view).getStaticAttributes(); assertEquals("value1", attributes.get("key1")); assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl()); attributes = ((InternalResourceView) view).getStaticAttributes(); assertEquals("value1", attributes.get("key1"));
@Test public void eagerInitialization() throws Exception { ResourceBundleViewResolver rb = new ResourceBundleViewResolver(); rb.setBasename(PROPS_FILE); rb.setCache(getCache()); rb.setDefaultParentView("testParent"); rb.setLocalesToInitialize(new Locale[] {Locale.ENGLISH, Locale.FRENCH}); rb.setApplicationContext(wac); View v = rb.resolveViewName("debugView", Locale.FRENCH); assertThat(v, instanceOf(InternalResourceView.class)); InternalResourceView jv = (InternalResourceView) v; assertEquals("French debugView must have correct URL", "jsp/debug/deboug.jsp", jv.getUrl()); assertEquals("Correct overridden (XML) content type", "text/xml;charset=ISO-8859-1", jv.getContentType()); }
@Test public void freeMarkerViewResolver() throws Exception { MockServletContext sc = new MockServletContext(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configurer.setServletContext(sc); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.getBeanFactory().registerSingleton("configurer", configurer); wac.refresh(); FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix"); vr.setApplicationContext(wac); View view = vr.resolveViewName("test", Locale.CANADA); assertEquals("Correct view class", FreeMarkerView.class, view.getClass()); assertEquals("Correct URL", "prefix_test_suffix", ((FreeMarkerView) view).getUrl()); view = vr.resolveViewName("non-existing", Locale.CANADA); assertNull(view); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
/** * Prepare for rendering, and determine the request dispatcher path * to forward to (or to include). * <p>This implementation simply returns the configured URL. * Subclasses can override this to determine a resource to render, * typically interpreting the URL in a different manner. * @param request current HTTP request * @param response current HTTP response * @return the request dispatcher path to use * @throws Exception if preparations failed * @see #getUrl() */ protected String prepareForRendering(HttpServletRequest request, HttpServletResponse response) throws Exception { String path = getUrl(); if (this.preventDispatchLoop) { String uri = request.getRequestURI(); if (path.startsWith("/") ? uri.equals(path) : uri.equals(StringUtils.applyRelativePath(uri, path))) { throw new ServletException("Circular view path [" + path + "]: would dispatch back " + "to the current handler URL [" + uri + "] again. Check your ViewResolver setup! " + "(Hint: This may be the result of an unspecified view, due to default view name generation.)"); } } return path; }
throw new ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!"); response.setContentType(getContentType()); if (logger.isDebugEnabled()) { logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'"); logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");